通过UrlRewriter在MVC4中配置伪静态在网上都有很多资料,本篇博客主要是把项目中使用到的通过UrlRewriter配置伪静态提取出来,方便自己以后查看,同样对需要该功能的网友提供思路。
第一:下载UrlRewriter.dll文件,并引用到项目中
第二:配置Web.config
依次在下面节点中添加UrlRewriter相关的子节点
- <configSections>
- <section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter"/>
- </configSections>
- <system.web>
- <httpModules>
- <add type="URLRewriter.RewriterModule, URLRewriter" name="RewriterModule"/>
- </httpModules>
- </system.web>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true">
- <add name="URLRewriter" type="URLRewriter.RewriterModule" preCondition="managedHandler"/>
- </modules>
- </system.webServer>
第三:配置RouteConfig.cs
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
-
- routes.MapRoute(
- "Action1Html",
- "{controller}/{action}.html",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- routes.MapRoute(
- "IDHtml",
- "{controller}/{action}/{id}.html",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
-
- routes.MapRoute(
- "ActionHtml",
- "{controller}/{action}.html/{id}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
-
- routes.MapRoute(
- "ControllerHtml",
- "{controller}.html/{action}/{id}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- routes.MapRoute(
- "Root",
- "",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional });
-
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
- }
第四:配置IIS
配置完Web.config,基本就可以直接访问地址了,如果是发布到IIS7还是要进行配置,具体配置查看如下地址:
http://www.cnblogs.com/zhongweiv/archive/2011/10/29/UrlRewriter_IIS.html