最近在做一个cms手机网站,由于内容不是很多,很快做完了。空闲的时间考虑seo,想把url全部写成静态的。在查资料的时候,发现 UrlRewrite.Net
这个组件非常的不错,尝试了下结果达到了理想的效果。
1、通过nuget
安装该类库;
2、注册为后缀html的路由;
3、web.config配置;
<!--在 configuration 节点下配置-->
<configSections>
<section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter" />
</configSections><br><br>
<!--在 system.webServer 节点下配置-->
<modules runAllManagedModulesForAllRequests="true">
<remove name="UrlRoutingModule" />
<add name="UrlRoutingModule" type="UrlRewrite.RewriteModule, UrlRewrite" preCondition="managedHandler" />
</modules>
4、生成url链接的时候添加上后缀,如果使用,如果是使用 @Html.ActionLink("主页", "Index", "Home")
这方式生成,会自动生成带后缀.html;
5、测试访问;
到此就可以了,这样比较有利于seo;
URL Routing
和URL Rewriting
两者之间的不同
- URL
Rewrite
是在request
处理之前修改相应的URL,URLRewrite
模块本身不知道哪个HttpHandler
处理这个请求,并且处理请求的HttpHandler
也不知道自己处理的URL是原来的URL还是被重写过的地址。 - 和URL
Rewrite
正好相反,URLRouting
是根据规则为URL来指定HttpHandler
的,可以看做Routing
是handler
的高级映射。 - IIS URL
Rewrite
可以用于任何web程序的映射处理,包括但不限于asp.net,php,asp和静态文件等,但Routing
只能处理基于.net的web程序。 - IIS URL
Rewrite
在IIS集成模式和经典模式都可以用,但默认情况下Routing
只能用在集成模式下,经典模式的话需要使用扩展名或者通配符将程序映射到IIS上(其实.net有另外一个组件aspnet filter已经帮我们做好了)。 - IIS URL
Rewrite
可以根据域名,路径,Http Header
,server
变量等处理重写规则,但Routing
只能按照请求的URL路径和HTTP-Method header
来处理。 - IIS URL
Rewrite
可以执行处理HTTP跳转,处理自定义的Status Code
以及Abort
请求,但Routing
做不到这些。 - IIS URL
Rewrite
的扩展只能扩展自己的Provider
来自定义规则的存储,但Routing
的扩展相对来说就非常强大了,MVC就是其中一种。
UrlRouting
(URL路由)和UrlRewrite
(URL重写)实现友好URL
URL重写已经不是什么新鲜事物了,URL重写实现URL搜索引擎友好化是SEO的一个重要环节,不管是ASP.NET程序,还是其他语言都可以写出一段代码来截取传入的HTTP请求并自动将该请求重定向到其他资源。使用传统的 ASP,应用 URL 重写的唯一方法是编写 ISAPI 筛选器,或者购买提供 URL 重写功能的第三方产品。但是,使用ASP.NET,您可以通过很多方法来轻松地创建您自己的 URL 重写引擎。
UrlRouting
(URL路由)是为了让Url更简短更直观更有意义才出现的,同时还可以通过参数得到重写后的Url在页面上使用。
如:原来ASP.NET中的参数URL:post.aspx?year=2010&month=6&day=1
, 使用UrlRouting
来配置一些URL的映射得到搜索引擎友好的,对用户也友好的url:/post/2010/6/1/
UrlRouting
早已在Asp.NET MVC项目中被广泛使用,ASP.NET 4.0中正式普遍到webform,UrlRouting
技术后于Url重写出现,在Asp.NET MVC 中UrlRouting
可以双向转换,既可以做url重写,还可以根据一些参数获得重写后的Url地址,但是它也有自己的不足之处,比如说如果你想连域名一起重写,比如博客地址elock.cnblogs.com
这样的重写,UrlRouting
就做不到了,只能用UrlRewrite
。
关于UrlRoutingModule
处理模块用于解析请求的URL,并选择相应的路由。
很多人在看源码的时候 对下面的context.RewritePath("~/UrlRouting.axd");
不解
public virtual void PostResolveRequestCache(HttpContextBase context)
{
RouteData routeData = this.RouteCollection.GetRouteData(context);
if (routeData != null)
{
IRouteHandler routeHandler = routeData.RouteHandler;
if (routeHandler == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoRouteHandler, new object[0]));
}
if (!(routeHandler is StopRoutingHandler))
{
RequestContext requestContext = new RequestContext(context, routeData);
IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
if (httpHandler == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoHttpHandler, new object[] { routeHandler.GetType() }));
}
RequestData data2 = new RequestData {
OriginalPath = context.Request.Path,
HttpHandler = httpHandler
};
context.Items[_requestDataKey] = data2;
context.RewritePath("~/UrlRouting.axd");
}
}
}
其实这里涉及到ASP.NET的HTTP请求的管线问题
大家都知道 PostResolveRequestCache
和PostMapRequestHandler
还有个事件 ,事件顺序如下(来自MSDN)
9.引发 PostResolveRequestCache
事件。
10.根据所请求资源的文件扩展名(在应用程序的配置文件中映射),选择实现 IHttpHandler
的类,对请求进行处理。如果该请求针对从Page
类派生的对象(页),并且需要对该页进行编译,则 ASP.NET会在创建该页的实例之前对其进行编译。
11.引发 PostMapRequestHandler
事件。
这个事件负责根据文件扩展名映射到具体的httphandle处理类,而MVC的URL信息没有具体的文件后缀名 为了使处理模块能够在iis7中实现路由,则采取了这么一种简单的解决办法。先把路径指向~/UrlRouting.axd
,在此事件中会设置一个UrlRouting.axd
类型的Handler
避免报错,
并在下一步事件中替换掉此处的Handler
再把~/UrlRouting.axd
这个路径给改回来。
public virtual void PostMapRequestHandler(HttpContextBase context)
{
RequestData data = (RequestData) context.Items[_requestDataKey];
if (data != null)
{
context.RewritePath(data.OriginalPath);
context.Handler = data.HttpHandler;
}
}