大佬教程收集整理的这篇文章主要介绍了ASP.NET 页面双向静态化,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在上一篇博文中我已经详细介绍并实现了.html页面到.aspx页面的映射,当然这属于伪静态,而且是单向的。
按照这个逻辑,必然会造成循环请求,不断地产生子请求,请求流程如下图:
而我们预期的结果应该如下图,实际只请求两次。
5;">1. 修改CustomRouteHandler类,添加requestPath属性
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;">CustomhttpR_40_11845@odule : <span style="color: #2b91af;">IhttpR_40_11845@odule
{<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;">"@R_607_5352@n"</span>,<span style="color: #a31515;">"/" </span>+ routeUrl); app.Response.End(); } } <span style="color: blue;">public void </span>Dispose() { } }
}
如果你不太熟悉httpApplication的事件,可以参照:@L_801_2@
黄色标记的地方是添加的,其它配置不变。
5;">xml "" "" <5;">configuration<
<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;">httpR_40_11845@odules</span><span style="color: blue;">> <span style="BACkground-color: #ffff00;"><</span></span></pre>
BACkground-color: #ffff00;">5;">add "httpR_40_11845@odule" "outIng_Static_Page_Demo.WebModule.CustomhttpR_40_11845@odule,RoutIng_Static_Page_Demo"
5;">httpR_40_11845@odules 5;">system.web <5;">system.webServer <5;">modules requests"" <5;">remove "outIngModule" <5;">add "outIngModule" "m.Web.RoutIng.UrlRoutIngModule,System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"<span style="BACkground-color: #ffff00;"><</span></span></pre>
BACkground-color: #ffff00;">5;">add "httpR_40_11845@odule" "outIng_Static_Page_Demo.WebModule.CustomhttpR_40_11845@odule"
5;">modules <5;">handlers <5;">add "outIngHandler" "" "" "outIng.axd" "m.Web.httpForbiddenHandler,Version=2.0.0.0,PublicKeyToken=b03f5f7f11d50a3a"</</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的配置问题。在
5;">validation "false"
以上是大佬教程为你收集整理的ASP.NET 页面双向静态化全部内容,希望文章能够帮你解决ASP.NET 页面双向静态化所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。