加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET 页面双向静态化

发布时间:2020-12-15 21:25:36 所属栏目:asp.Net 来源:网络整理
导读:在上一篇博文中我已经详细介绍并实现了.html页面到.aspx页面的映射,当然这属于伪静态,而且是单向的。 按照这个逻辑,必然会造成循环请求,不断地产生子请求,请求流程如下图: 而我们预期的结果应该如下图,实际只请求两次。 System.Web; System.Web.Compi

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

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

image

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

image

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则是

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;"&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;"Location"</span>,<span style="color: #a31515;"&gt;"/" </span>+ routeUrl);
            app.Response.End();
        }
    }

    <span style="color: blue;"&gt;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;"&gt;httpModules</span><span style="color: blue;"&gt;>
  <span style="background-color: #ffff00;"&gt;<</span></span></pre>

"" ""


    
  
  <
    <""
      <""
      <"" "" 
  <span style="background-color: #ffff00;"&gt;<</span></span></pre>

"" ""


    
    <
      <"" 
                                  "" 
                                  "" ""
                                  ""
</</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的权限太高了。

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

SNAGHTML9af826

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

""

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读