页面静态化最大的好处是利于SEO,即便是伪静态,搜索引擎也会以为这是一个较为友好的Url。Url的友好也取决于其命名,为一篇描述古代文学的页面起名用ancient-literature.html固然比随便起的名字例如aa.html之流要友好。页面静态化并不表明你必定要用后缀名为.html或.htm的连接来显示你的页面,你彻底能够不用任何后缀名(就像MVC同样),只要Url结构良好。html
实现静态化的三个目标:web
1. 实现页面静态化,页面中的连接都用.html来表示,但每一个.html实际都映射了一个.aspx页面。windows
例如:当用户请求index.html页面时,实际请求的是Default.aspx页面,index.html的物理路径在网站中并不存在。服务器
2. 实现请求.aspx页面时自动跳转到对应的静态映射页面。app
例如:当用户请求Default.aspx页面,自动跳重定向到index.html页面ide
3. 自定义404页面的实现网站
当请求的路径既不在映射表中,也不在网站的虚拟路径中时,它将自动跳转到我预先设定好的404页面。ui
实现以上要点,须要用到ASP.NET Url Routing、HttpHandler和HttpModule技术。this
这是一个小系列的文章,这一篇文章将详细解释并实现第1点。搜索引擎
1、项目建立
1. 建立一个ASP.NET Web Application项目
2. 建立web.config文件
ASP.NET Membership在这里使用不到,因此生成的web.config配置没有用处,删掉它并从新建立一个新的web.config文件
3. 将网站添加到IIS6或IIS7中
默认的ASP.NET Web Application已经为咱们提供了很多页面,我就在下面的例子中将它们静态化吧。
2、页面静态化实现
1. 添加Routing引用
因为这里须要用到ASP.NET的路由映射(从.NET 3.5开始诞生),因此须要在项目中添加System.Web.Routing引用。
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实现
usingSystem.Web;
usingSystem.Web.Compilation;
usingSystem.Web.Routing;
usingSystem.Web.UI;
namespaceRouting_Static_Page_Demo.WebHandler
{
public classCustomRouteHandler: IRouteHandler{
///
///虚拟路径/// public stringVirtualPath { get; private set; }
publicCustomRouteHandler(stringvirtualPath)
{
this.VirtualPath = virtualPath;
}
///
///返回实际请求页/// publicIHttpHandlerGetHttpHandler(RequestContextrequestContext)
{
varpage = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) asIHttpHandler;
returnpage;
}
}
}
5. 在Global.asax文件中注册路由
先来个简单的实现:
usingSystem;
usingSystem.IO;
usingSystem.Web.Routing;
usingRouting_Static_Page_Demo.WebHandler;
namespaceRouting_Static_Page_Demo
{
public classGlobal: System.Web.voidApplication_Start(objectsender, EventArgse)
{
RegisterRoutes();
}
///
///注册路由/// private voidRegisterRoutes()
{
//将Index.html请求映射为Default.aspxRouteTable.Routes.Add("Default",
newRoute("Index.html",
newCustomRouteHandler("~/Default.aspx")));
// 将About.html请求映射为About.aspxRouteTable.Routes.Add("About",
newRoute("About.html",
newCustomRouteHandler("~/About.aspx")));
// 将Account/Login.html请求映射为/Account/Login.aspxRouteTable.Routes.Add("Login",
newRoute("Account/Login.html",
newCustomRouteHandler("~/Account/Login.aspx")));
}
}
}
在VS中直接运行站点(VS自带的WebDev服务器),点击这些连接都可以正常访问。
三. 在IIS 7下设置站点
下面的设置很重要,由于上面在VS自带的web服务器中虽然跑通了,但IIS 7下是运行不经过的(IIS 6下的设置很简单,本文的在线Demo是运行在IIS 6下的)
1. 初次在IIS 7下运行该网站,会出现下面的错误。
这是由于IIS对该Web站点目录没有读写权限。
在IIS下:右键站点 > Edit Permissions > Security > Edit > Add > 输入IIS_IUSRS > Check Names > OK。
选择完毕后,为IIS_IUSRS用户添加Full Control权限。
border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="SNAGHTML2f152df[4]" border="0" alt="SNAGHTML2f152df[4]" src="http://www.jileiba.com/wp-content/uploads/2012/03/SNAGHTML2f152df4_thumb.png" width="381" height="460" />
2. 添加完该设置后,再运行一次网站,可能会出现下面的错误。
按照上面的步骤添加IUSR用户,为IUSR用户分配Read权限便可。
再次运行网站,可以正常访问页面了。
3. 配置web.config
网站虽然能运行,可是点击Home或About连接时会出现404错误。
i.首先确保在安装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
preCondition="integratedMode"
verb="*" path="UrlRouting.axd"
type="System.Web.HttpForbiddenHandler, System.Web,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
注意: 若是你采用的是ASP.NET 3.5 Routing或使用IIS 6,web.config配置会不同。
iii.确保web站点的应用程序池选择的是集成模式,由于ASP.NET 4.0 Routing并不支持经典模式
OK,彷佛全部的该配置的地方都配置了,那么再去点击Index.html或About.html连接试试吧。
若是如今去访问Login.html页面,仍是会获得一个401.3的错误,更改Account目录下的web.config文件:
若是你不须要这个web.config文件,直接删掉也能够。
四. 更改RegisterRoutes方法
上面提供的注册路由的方式属于硬编码,须要为每个.aspx页面指定映射路由。Account目录下还有一些.aspx文件,若是增长别的目录也存放.aspx页面,为了让每一个页面都静态化,RegisterRoutes方法将会是产生不少重复代码。
usingSystem;
usingSystem.IO;
usingSystem.Web.Routing;
usingRouting_Static_Page_Demo.WebHandler;
namespaceRouting_Static_Page_Demo
{
public classGlobal: System.Web.HttpApplication{
voidApplication_Start(objectsender, EventArgse)
{
RegisterRoutes();
}
///
///注册路由/// private voidRegisterRoutes()
{
//将Index.html请求映射为Default.aspxRouteTable.Routes.Add("Default",
newRoute("Index.html",
newCustomRouteHandler("~/Default.aspx")));
// 将About.html请求映射为About.aspxRouteTable.Routes.Add("About",
newRoute("About.html",
newCustomRouteHandler("~/About.aspx")));
// 遍历页面存放目录,为每一个.aspx页面添加路由映射foreach(stringmapPth in_pageMapPath)
{
stringpath = Server.MapPath(mapPth);
vardirectoryInfo = newDirectoryInfo(path);
foreach(FileInfof indirectoryInfo.GetFiles())
{
stringfileName = f.Name;
if(fileName.EndsWith(".aspx"))
{
stringrouteName = fileName.Substring(0, fileName.Length - 5);
stringurl = string.Concat(mapPth.Substring(2), routeName, ".html");
RouteTable.Routes.Add(routeName,
newRoute(url,
newCustomRouteHandler(string.Concat(mapPth, fileName))));
}
}
}
}
// 页面存放目录private readonly string[] _pageMapPath = {@"~/Account/"};
}
}
以上代码就能实现为每一个.aspx页面注册路由实现静态化。