读书人

ASP.NET中兑现二级或多级域名(URLRewr

发布时间: 2013-09-08 15:21:21 作者: rapoo

ASP.NET中实现二级或多级域名(URLRewriter)
1.URLRewriter.DLL两个文件已经修改好,生成了复制到了Bin文件夹

BaseModuleRewriter.cs

C# code
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Url.AbsoluteUri, app);
}


BaseModuleRewriter.cs

C# code
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
// log information to the Trace object.
app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");

// get the configuration rules
RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

// iterate through each rule...
for(int i = 0; i < rules.Count; i++)
{
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
string lookFor = "^" + rules[i].LookFor + "$";

// Create a regex (note that IgnoreCase is set...)
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

// See if a match is found
if (re.IsMatch(requestedPath))
{


// match found - do any replacement needed
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));

// log rewriting information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);

// Rewrite the URL
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
break; // exit the for loop
}
}




2.已经做了泛解析

3.80端口主机头为空的站点

4.添加ASPNET_ISAPI的通配符应用程序映射,取消"确认文件是否存在"前的钩.

5.服务器是独立IP


规则,比如域名是abc.com
<RewriterRule>
<LookFor>http://(\d*)\.abc\.com/</LookFor>
<SendTo>~/2.aspx?id=$1</SendTo>
</RewriterRule>

.aspx页面
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/2.aspx?id=2">正常URL</asp:HyperLink>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="http://2.abc.com/">重写URL</asp:HyperLink>

正常访问 正常URL 参数是 返回参数 2
正常访问 重写URL 提示 http 400错误 无法找到该网页


[解决办法]
通过urlrewriter重写路径


<RewriterRule>
<LookFor>http://(\d+)\.abc\.com/ </LookFor>
<SendTo>/show.aspx?id=$1 </SendTo>
</RewriterRule>
[解决办法]
??2.已经做了泛解析
不知道LZ是怎么做的呢?不在域名服务商那里能做解释吗?,每个域名的子域名都是有限的,所以不建议用子域名来做参数使用,太浪费

读书人网 >asp.net

热点推荐