asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ASP.NET 页面双向静态化大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

在上一篇博文中我已经详细介绍并实现了.html页面到.aspx页面的映射,当然这属于伪静态,而且是单向的。

按照这个逻辑,必然会造成循环请求,不断地产生子请求,请求流程如下图:

ASP.NET 页面双向静态化

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

ASP.NET 页面双向静态化

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;"&gt;/// <sumMary>
    /// </span><span style="color: green;"&gt;请求路径
    </span><span style="color: gray;"&gt;/// </sumMary>
    </span><span style="color: blue;"&gt;public String </span>requestPath
    {
        <span style="color: blue;"&gt;get </span>{ <span style="color: blue;"&gt;return </span>VirtualPath.SubString(1); }
    }

    <span style="color: blue;"&gt;public </span>CustomRouteHandler(<span style="color: blue;"&gt;String </span>virtualPath)
    {
        <span style="color: blue;"&gt;this</span>.VirtualPath = virtualPath;
    }

    <span style="color: gray;"&gt;/// <sumMary>
    /// </span><span style="color: green;"&gt;返回实际请求页
    </span><span style="color: gray;"&gt;/// </sumMary>
    </span><span style="color: blue;"&gt;public </span><span style="color: #2b91af;"&gt;IhttpHandler </span>GethttpHandler(<span style="color: #2b91af;"&gt;requestContext </span>requestContext)
    {
        <span style="color: blue;"&gt;foreach </span>(<span style="color: blue;"&gt;var </span>urlParm <span style="color: blue;"&gt;in </span>requestContext.RouteData.Values)
        {
            requestContext.httpContext.Items[urlParm.Key] = urlParm.Value;
        }
        <span style="color: blue;"&gt;var </span>page = <span style="color: #2b91af;"&gt;BuildManager</span>.CreateInstanceFromVirtualPath(VirtualPath,<span style="color: blue;"&gt;typeof</span>(<span style="color: #2b91af;"&gt;Page</span>)) <span style="color: blue;"&gt;as </span><span style="color: #2b91af;"&gt;IhttpHandler</span>;

        <span style="color: blue;"&gt;return </span>page;
    }
}

}

requestPath属性是从VirtualPath过来的,如果VirtualPath为,那么对应的requestPath则是

httpR_40_11845@odule.cs类

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;"&gt;private </span><span style="color: #2b91af;"&gt;httpApplication </span>app;

    <span style="color: blue;"&gt;public void </span>Init(<span style="color: #2b91af;"&gt;httpApplication </span>context)
    {
        app = context;
        app.Authorizerequest += App_Authorizerequest;
    }

    <span style="color: blue;"&gt;public void </span>App_Authorizerequest(<span style="color: blue;"&gt;object </span>sender,<span style="color: #2b91af;"&gt;EventArgs </span>E)
    {
        <span style="color: #2b91af;"&gt;httprequest </span>req = app.request;
        <span style="color: blue;"&gt;String </span>path = req.Path;

        <span style="color: green;"&gt;// 如果是.aspx页面
        </span><span style="color: blue;"&gt;if </span>(path.EndsWith(<span style="color: #a31515;"&gt;".aspx"</span>,<span style="color: blue;"&gt;true</span>,<span style="color: #2b91af;"&gt;CultureInfo</span>.CurrentCulturE))
        {

            <span style="color: green;"&gt;// routeUrl则用于存放对应的.html
            </span><span style="color: blue;"&gt;String </span>routeUrl = <span style="color: blue;"&gt;String</span>.Empty;

            <span style="color: green;"&gt;// 遍历RouteTable,找到.aspx页面对应的.html
            </span><span style="color: blue;"&gt;foreach </span>(<span style="color: #2b91af;"&gt;Route </span>route <span style="color: blue;"&gt;in </span><span style="color: #2b91af;"&gt;RouteTable</span>.Routes)
            {
                <span style="color: green;"&gt;// 获取CustomRouteHandler
                </span><span style="color: blue;"&gt;var </span>handler = (<span style="color: #2b91af;"&gt;CustomRouteHandler</span>) route.RouteHandler;
                <span style="color: green;"&gt;// 获取CustomRouteHandler的requestPath
                </span><span style="color: blue;"&gt;String </span>requestPath = handler.requestPath;

                <span style="color: blue;"&gt;if </span>(requestPath.ToLower() == path.ToLower())
                {
                    routeUrl = route.Url;
                    <span style="color: blue;"&gt;break</span>;
                }
            }

            <span style="color: green;"&gt;// 将.aspx页面永久重定向到对应的.html页面
            </span>app.Response.StatusCode = 301;
            app.Response.AddHeader(<span style="color: #a31515;"&gt;"@R_607_5352@n"</span>,<span style="color: #a31515;"&gt;"/" </span>+ routeUrl);
            app.Response.End();
        }
    }

    <span style="color: blue;"&gt;public void </span>Dispose()
    {

    }
}

}

如果你不太熟悉httpApplication的事件,可以参照:@L_801_2@

httpR_40_11845@odule配置

黄色标记的地方是添加的,其它配置不变。

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;"&gt;httpR_40_11845@odules</span><span style="color: blue;"&gt;>
  <span style="BACkground-color: #ffff00;"&gt;<</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;"&gt;<</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;"&gt;handlers</span><span style="color: blue;"&gt;>

</<span style="color: #a31515;">system.webServer<span style="color: blue;">>

</<span style="color: #a31515;">configuration<span style="color: blue;">>

在VS自带的WebDev服务器中运行这个项目:在浏览器栏输入,会自动跳转到,运行默认路径也会自动跳转到

WebDev运行然通过了,IIS可不见得通过,毕竟WebDev的权限太高了。

果然,运行之后,出现下面的错误画面:

ASP.NET 页面双向静态化

还是web.config的配置问题。在节点下添加下面一行配置:

5;">validation "false"

大佬总结

以上是大佬教程为你收集整理的ASP.NET 页面双向静态化全部内容,希望文章能够帮你解决ASP.NET 页面双向静态化所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: