在上一篇博文中我已经详细介绍并实现了.html页面到.aspx页面的映射,当然这属于伪静态,而且是单向的。
按照这个逻辑,必然会造成循环请求,不断地产生子请求,请求流程如下图:

而我们预期的结果应该如下图,实际只请求两次。

System.Web;
System.Web.Compilation;
System.Web.Routing;
System.Web.UI;
<span style="color: blue;">namespace Routing_Static_Page_Demo.WebHandler
{
<span style="color: blue;">public class <span style="color: #2b91af;">CustomRouteHandler : <span style="color: #2b91af;">IRouteHandler
{
<span style="color: gray;">///
/// <span style="color: green;">虚拟路径
<span style="color: gray;">///
<span style="color: blue;">public string VirtualPath { <span style="color: blue;">get; <span style="color: blue;">private set; }
<span style="color: gray;">/// <summary>
/// </span><span style="color: green;">请求路径
</span><span style="color: gray;">/// </summary>
</span><span style="color: blue;">public string </span>RequestPath
{
<span style="color: blue;">get </span>{ <span style="color: blue;">return </span>VirtualPath.Substring(1); }
}
<span style="color: blue;">public </span>CustomRouteHandler(<span style="color: blue;">string </span>virtualPath)
{
<span style="color: blue;">this</span>.VirtualPath = virtualPath;
}
<span style="color: gray;">/// <summary>
/// </span><span style="color: green;">返回实际请求页
</span><span style="color: gray;">/// </summary>
</span><span style="color: blue;">public </span><span style="color: #2b91af;">IHttpHandler </span>GetHttpHandler(<span style="color: #2b91af;">RequestContext </span>requestContext)
{
<span style="color: blue;">foreach </span>(<span style="color: blue;">var </span>urlParm <span style="color: blue;">in </span>requestContext.RouteData.Values)
{
requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value;
}
<span style="color: blue;">var </span>page = <span style="color: #2b91af;">BuildManager</span>.CreateInstanceFromVirtualPath(VirtualPath,<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Page</span>)) <span style="color: blue;">as </span><span style="color: #2b91af;">IHttpHandler</span>;
<span style="color: blue;">return </span>page;
}
}
}
RequestPath属性是从VirtualPath过来的,如果VirtualPath为,那么对应的RequestPath则是
System;
System.Globalization;
System.Web;
System.Web.Routing;
Routing_Static_Page_Demo.WebHandler;
<span style="color: blue;">namespace Routing_Static_Page_Demo.WebModule
{
<span style="color: blue;">public class <span style="color: #2b91af;">CustomHttpModule : <span style="color: #2b91af;">IHttpModule
{
<span style="color: blue;">private </span><span style="color: #2b91af;">HttpApplication </span>app;
<span style="color: blue;">public void </span>Init(<span style="color: #2b91af;">HttpApplication </span>context)
{
app = context;
app.AuthorizeRequest += App_AuthorizeRequest;
}
<span style="color: blue;">public void </span>App_AuthorizeRequest(<span style="color: blue;">object </span>sender,<span style="color: #2b91af;">EventArgs </span>e)
{
<span style="color: #2b91af;">HttpRequest </span>req = app.Request;
<span style="color: blue;">string </span>path = req.Path;
<span style="color: green;">// 如果是.aspx页面
</span><span style="color: blue;">if </span>(path.EndsWith(<span style="color: #a31515;">".aspx"</span>,<span style="color: blue;">true</span>,<span style="color: #2b91af;">CultureInfo</span>.CurrentCulture))
{
<span style="color: green;">// routeUrl则用于存放对应的.html
</span><span style="color: blue;">string </span>routeUrl = <span style="color: blue;">string</span>.Empty;
<span style="color: green;">// 遍历RouteTable,找到.aspx页面对应的.html
</span><span style="color: blue;">foreach </span>(<span style="color: #2b91af;">Route </span>route <span style="color: blue;">in </span><span style="color: #2b91af;">RouteTable</span>.Routes)
{
<span style="color: green;">// 获取CustomRouteHandler
</span><span style="color: blue;">var </span>handler = (<span style="color: #2b91af;">CustomRouteHandler</span>) route.RouteHandler;
<span style="color: green;">// 获取CustomRouteHandler的RequestPath
</span><span style="color: blue;">string </span>requestPath = handler.RequestPath;
<span style="color: blue;">if </span>(requestPath.ToLower() == path.ToLower())
{
routeUrl = route.Url;
<span style="color: blue;">break</span>;
}
}
<span style="color: green;">// 将.aspx页面永久重定向到对应的.html页面
</span>app.Response.StatusCode = 301;
app.Response.AddHeader(<span style="color: #a31515;">"Location"</span>,<span style="color: #a31515;">"/" </span>+ routeUrl);
app.Response.End();
}
}
<span style="color: blue;">public void </span>Dispose()
{
}
}
}
如果你不太熟悉HttpApplication的事件,可以参照: 如果你不太熟悉HttpApplication的用法,可以参照:
黄色标记的地方是添加的,其它配置不变。
"" ""
<
< <span style="color: #a31515;">system.web<span style="color: blue;">>
<<span style="color: #a31515;">compilation <span style="color: red;">debug<span style="color: blue;">="<span style="color: blue;">true" <span style="color: red;">targetFramework<span style="color: blue;">="<span style="color: blue;">4.0" <span style="color: blue;">/>
<</span><span style="color: #a31515;">httpModules</span><span style="color: blue;">>
<span style="background-color: #ffff00;"><</span></span></pre>
"" ""
<
<""
<""
<"" ""
<span style="background-color: #ffff00;"><</span></span></pre>
"" ""
<
<""
""
"" ""
""
</</span><span style="color: #a31515;">handlers</span><span style="color: blue;">>
</ <span style="color: #a31515;">system.webServer<span style="color: blue;">>
</ <span style="color: #a31515;">configuration<span style="color: blue;">>
在VS自带的WebDev服务器中运行这个项目:在浏览器栏输入,会自动跳转到,运行默认路径也会自动跳转到。
WebDev运行虽然通过了,IIS可不见得通过,毕竟WebDev的权限太高了。
果然,运行之后,出现下面的错误画面:

还是web.config的配置问题。在节点下添加下面一行配置:
""
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|