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

ASP.NET 路由实现页面静态化(附在线Demo和Source)

发布时间:2020-12-15 21:25:24 所属栏目:asp.Net 来源:网络整理
导读:页面静态化最大的好处是利于SEO,即使是伪静态,搜索引擎也会觉得这是一个较为友好的Url。Url的友好也取决于其命名,为一篇描述古代文学的页面起名用ancient-literature.html当然比随便起的名字例如aa.html之流要友好。页面静态化并不代表你一定要用后缀名为

页面静态化最大的好处是利于SEO,即使是伪静态,搜索引擎也会觉得这是一个较为友好的Url。Url的友好也取决于其命名,为一篇描述古代文学的页面起名用ancient-literature.html当然比随便起的名字例如aa.html之流要友好。页面静态化并不代表你一定要用后缀名为.html或.htm的链接来显示你的页面,你完全可以不用任何后缀名(就像MVC一样),只要Url结构良好。

实现静态化的三个目标:

1. 实现页面静态化,页面中的链接都用.html来表示,但每个.html实际都映射了一个.aspx页面。

例如:当用户请求index.html页面时,实际请求的是Default.aspx页面,index.html的物理路径在网站中并不存在。

2. 实现请求.aspx页面时自动跳转到对应的静态映射页面。

例如:当用户请求Default.aspx页面,自动跳重定向到index.html页面

3. 自定义404页面的实现,当请求的路径既不在映射表中,也不在网站的虚拟路径中时,它将自动跳转到我预先设定好的404页面。

实现以上要点,需要用到ASP.NET Url Routing、HttpHandler和HttpModule技术。

这是一个小系列的文章,这一篇文章将详细解说并实现第1点。

本文已经同步至我的个人博客站点:|

源代码下载:

在线Demo:

一、项目创建

1. 创建一个ASP.NET Web Application项目

SNAGHTMLf9ef62

image

2. 创建web.config文件

ASP.NET Membership在这里使用不到,所以生成的web.config配置没有用处,删掉它并重新创建一个新的web.config文件

""
<
<</span><span style="color: #a31515;"&gt;system.web</span><span style="color: blue;"&gt;>
    <</span><span style="color: #a31515;"&gt;compilation </span><span style="color: red;"&gt;debug</span><span style="color: blue;"&gt;=</span>"<span style="color: blue;"&gt;true</span>" <span style="color: red;"&gt;targetFramework</span><span style="color: blue;"&gt;=</span>"<span style="color: blue;"&gt;4.0</span>" <span style="color: blue;"&gt;/>
</</span><span style="color: #a31515;"&gt;system.web</span><span style="color: blue;"&gt;>

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

3. 将网站添加到IIS6或IIS7中

SNAGHTML1048e8d

默认的ASP.NET Web Application已经为我们提供了不少页面,我就在下面的例子中将它们静态化吧。

二、页面静态化实现

1. 添加Routing引用

由于这里需要用到ASP.NET的路由映射(从.NET 3.5开始诞生),所以需要在项目中添加System.Web.Routing引用。

SNAGHTMLfc9503

SNAGHTML1007756

2. 添加WebHandler和WebModule文件夹

这两个文件夹分别用于存放IHttpHandler和IHttpModule。

3. 将所有.aspx后缀的超链接更改为.html

Site.Master文件:


        <

Account文件夹ChangePassword.aspx文件:

当然现在这三个静态链接都访问不到,因为它们的物理地址不存在。

下面我们要做的就是:

1) 请求Index.html时实际请求的是Default.aspx

2) 请求About.html时实际请求的是About.aspx

3) 请求Account/Login.html时实际请求的是Account/Login.aspx

4. 添加自定义的IRouteHandler实现

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: 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;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;
    }
}

}

5. 在Global.asax文件中注册路由

先来个简单的实现:

System;
System.IO;
System.Web.Routing;
Routing_Static_Page_Demo.WebHandler;

<span style="color: blue;">namespace Routing_Static_Page_Demo
{
<span style="color: blue;">public class <span style="color: #2b91af;">Global : System.Web.<span style="color: #2b91af;">HttpApplication
{

    <span style="color: blue;"&gt;void </span>Application_Start(<span style="color: blue;"&gt;object </span>sender,<span style="color: #2b91af;"&gt;EventArgs </span>e)
    {
        RegisterRoutes();
    }

    <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;private void </span>RegisterRoutes()
    {

        <span style="color: green;"&gt;//将Index.html请求映射为Default.aspx
        </span><span style="color: #2b91af;"&gt;RouteTable</span>.Routes.Add(<span style="color: #a31515;"&gt;"Default"</span>,<span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;Route</span>(<span style="color: #a31515;"&gt;"Index.html"</span>,<span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;CustomRouteHandler</span>(<span style="color: #a31515;"&gt;"~/Default.aspx"</span>)));

        <span style="color: green;"&gt;// 将About.html请求映射为About.aspx
        </span><span style="color: #2b91af;"&gt;RouteTable</span>.Routes.Add(<span style="color: #a31515;"&gt;"About"</span>,<span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;Route</span>(<span style="color: #a31515;"&gt;"About.html"</span>,<span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;CustomRouteHandler</span>(<span style="color: #a31515;"&gt;"~/About.aspx"</span>)));

        <span style="color: green;"&gt;// 将Account/Login.html请求映射为/Account/Login.aspx
        </span><span style="color: #2b91af;"&gt;RouteTable</span>.Routes.Add(<span style="color: #a31515;"&gt;"Login"</span>,<span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;Route</span>(<span style="color: #a31515;"&gt;"Account/Login.html"</span>,<span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;CustomRouteHandler</span>(<span style="color: #a31515;"&gt;"~/Account/Login.aspx"</span>)));
    }
}

}

在VS中直接运行站点(VS自带的WebDev服务器),点击这些链接都能够正常访问。

三.? 在IIS 7下设置站点

下面的设置很重要,因为上面在VS自带的web服务器中虽然跑通了,但IIS 7下是运行不通过的(IIS 6下的设置很简单,本文的在线Demo是运行在IIS 6下的)

1. 初次在IIS 7下运行该网站,会出现下面的错误。

SNAGHTML2cedf45

这是因为IIS对该Web站点目录没有读写权限。

在IIS下:右键站点 >? Edit Permissions > Security > Edit > Add > 输入IIS_IUSRS > Check Names > OK。

选择完毕后,为IIS_IUSRS用户添加Full Control权限。

image

image

SNAGHTML2f152df[4]

2. 添加完该设置后,再运行一次网站,可能会出现下面的错误。

SNAGHTML2f65a2f

按照上面的步骤添加IUSR用户,为IUSR用户分配Read权限即可。

SNAGHTML2f8d75a

再次运行网站,能够正常访问页面了。

SNAGHTML2fd7e14

3.? 配置web.config

网站虽然能运行,但是点击Home或About链接时会出现404错误。

SNAGHTML2ff02fc

首先确保在安装IIS时你已经勾选了HTTP Reirection

如果没有安装这个功能,按照如下设置再配置一遍IIS

Control Panel --> Progams --> Turn off windows features --> World wide web Services --> Common HTTP Features –> HTTP Redirection

ii. 修改web.config文件,在webserver中注册RoutingHandler和RoutingModule

"" ""
<

<<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 style="color: #a31515;">system.web<span style="color: blue;">>
<<span style="color: #a31515;">system.webServer<span style="color: blue;">>
<<span style="color: #a31515;">modules <span style="color: red;">runAllManagedModulesForAllRequests<span style="color: blue;">="<span style="color: blue;">true"<span style="color: blue;">>
<<span style="color: #a31515;">remove <span style="color: red;">name<span style="color: blue;">="<span style="color: blue;">UrlRoutingModule"<span style="color: blue;">/>
<<span style="color: #a31515;">add <span style="color: red;">name<span style="color: blue;">="<span style="color: blue;">UrlRoutingModule" <span style="color: red;">type<span style="color: blue;">="<span style="color: blue;">System.Web.Routing.UrlRoutingModule,System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" <span style="color: blue;">/>
</<span style="color: #a31515;">modules<span style="color: blue;">>
<<span style="color: #a31515;">handlers<span style="color: blue;">>
<<span style="color: #a31515;">add <span style="color: red;">name<span style="color: blue;">="<span style="color: blue;">UrlRoutingHandler"
<span style="color: red;">preCondition<span style="color: blue;">="<span style="color: blue;">integratedMode"
<span style="color: red;">verb<span style="color: blue;">="<span style="color: blue;">*" <span style="color: red;">path<span style="color: blue;">="<span style="color: blue;">UrlRouting.axd"
<span style="color: red;">type<span style="color: blue;">="<span style="color: blue;">System.Web.HttpForbiddenHandler,Version=2.0.0.0,PublicKeyToken=b03f5f7f11d50a3a"<span style="color: blue;">/>
</<span style="color: #a31515;">handlers<span style="color: blue;">>
</<span style="color: #a31515;">system.webServer<span style="color: blue;">>

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

注意: 如果你采用的是ASP.NET 3.5 Routing或使用IIS 6,web.config配置会不一样。

确保web站点的应用程序池选择的是集成模式,因为ASP.NET 4.0 Routing并不支持经典模式

SNAGHTML32bdeab

OK,似乎所有的该配置的地方都配置了,那么再去点击Index.html或About.html链接试试吧。

SNAGHTML32d4edb

如果现在去访问Login.html页面,还是会得到一个401.3的错误,更改Account目录下的web.config文件:

""
<

<<span style="color: #a31515;">location <span style="color: red;">path<span style="color: blue;">="<span style="color: blue;">Register.aspx"<span style="color: blue;">>
<<span style="color: #a31515;">system.web<span style="color: blue;">>
<<span style="color: #a31515;">authorization<span style="color: blue;">>
<<span style="color: #a31515;">allow <span style="color: red;">users<span style="color: blue;">="<span style="color: blue;">*"<span style="color: blue;">/>
</<span style="color: #a31515;">authorization<span style="color: blue;">>
</<span style="color: #a31515;">system.web<span style="color: blue;">>
</<span style="color: #a31515;">location<span style="color: blue;">>

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

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

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

如果你不需要这个web.config文件,直接删掉也可以。

四. 更改RegisterRoutes方法

上面提供的注册路由的方式属于硬编码,需要为每一个.aspx页面指定映射路由。Account目录下还有一些.aspx文件,如果增加别的目录也存放.aspx页面,为了让每个页面都静态化,RegisterRoutes方法将会是产生很多重复代码。

System;
System.IO;
System.Web.Routing;
Routing_Static_Page_Demo.WebHandler;

<span style="color: blue;">namespace Routing_Static_Page_Demo
{
<span style="color: blue;">public class <span style="color: #2b91af;">Global : System.Web.<span style="color: #2b91af;">HttpApplication
{

    <span style="color: blue;"&gt;void </span>Application_Start(<span style="color: blue;"&gt;object </span>sender,<span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;CustomRouteHandler</span>(<span style="color: #a31515;"&gt;"~/About.aspx"</span>)));

        <span style="color: green;"&gt;// 遍历页面存放目录,为每个.aspx页面添加路由映射
        </span><span style="color: blue;"&gt;foreach </span>(<span style="color: blue;"&gt;string </span>mapPth <span style="color: blue;"&gt;in </span>_pageMapPath)
        {
            <span style="color: blue;"&gt;string </span>path = Server.MapPath(mapPth);
            <span style="color: blue;"&gt;var </span>directoryInfo = <span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;DirectoryInfo</span>(path);
            <span style="color: blue;"&gt;foreach </span>(<span style="color: #2b91af;"&gt;FileInfo </span>f <span style="color: blue;"&gt;in </span>directoryInfo.GetFiles())
            {
                <span style="color: blue;"&gt;string </span>fileName = f.Name;
                <span style="color: blue;"&gt;if </span>(fileName.EndsWith(<span style="color: #a31515;"&gt;".aspx"</span>))
                {
                    <span style="color: blue;"&gt;string </span>routeName = fileName.Substring(0,fileName.Length - 5);
                    <span style="color: blue;"&gt;string </span>url = <span style="color: blue;"&gt;string</span>.Concat(mapPth.Substring(2),routeName,<span style="color: #a31515;"&gt;".html"</span>);
                    <span style="color: #2b91af;"&gt;RouteTable</span>.Routes.Add(routeName,<span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;Route</span>(url,<span style="color: blue;"&gt;new </span><span style="color: #2b91af;"&gt;CustomRouteHandler</span>(<span style="color: blue;"&gt;string</span>.Concat(mapPth,fileName))));
                }
            }
        }

    }

    <span style="color: green;"&gt;// 页面存放目录
    </span><span style="color: blue;"&gt;private readonly string</span>[] _pageMapPath = {<span style="color: #a31515;"&gt;@"~/Account/"</span>};
}

}

以上代码就能实现为每个.aspx页面注册路由实现静态化。

(编辑:李大同)

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

    推荐文章
      热点阅读