投稿 评论 顶部

ASP.NET MVC4通过UrlRewriter配置伪静态

佚名 网络安全

通过UrlRewriter在MVC4中配置伪静态在网上都有很多资料,本篇博客主要是把项目中使用到的通过UrlRewriter配置伪静态提取出来,方便自己以后查看,同样对需要该功能的网友提供思路。

第一:下载UrlRewriter.dll文件,并引用到项目中

第二:配置Web.config

依次在下面节点中添加UrlRewriter相关的子节点

[html]? view plain ?copy
  1. <configSections>??
  2. <section?name="CustomConfiguration"?type="URLRewriter.Config.UrlsSection,?URLRewriter"/>??
  3. </configSections>??

[html]? view plain ?copy
  1. <system.web>??
  2. ????<httpModules>??
  3. ??????<add?type="URLRewriter.RewriterModule,?URLRewriter"?name="RewriterModule"/>??
  4. ????</httpModules>??
  5. ??</system.web>??
[html]? view plain ?copy
  1. <system.webServer>??
  2. ????<modules?runAllManagedModulesForAllRequests="true">??
  3. ??????<add?name="URLRewriter"?type="URLRewriter.RewriterModule"?preCondition="managedHandler"/>??
  4. ????</modules>??
  5. ??</system.webServer>??

第三:配置RouteConfig.cs

[csharp]? view plain ?copy
  1. public?static?void?RegisterRoutes(RouteCollection?routes)??
  2. ????????{??
  3. ????????????routes.IgnoreRoute("{resource}.axd/{*pathInfo}");??
  4. ??
  5. ????????????routes.MapRoute(??
  6. ???????????????"Action1Html",?//?action伪静态????
  7. ???????????????"{controller}/{action}.html",//?带有参数的?URL????
  8. ???????????????new?{?controller?=?"Home",?action?=?"Index",?id?=?UrlParameter.Optional?}//?参数默认值????
  9. ???????????);??
  10. ????????????routes.MapRoute(??
  11. ???????????????"IDHtml",?//?id伪静态????
  12. ???????????????"{controller}/{action}/{id}.html",//?带有参数的?URL????
  13. ???????????????new?{?controller?=?"Home",?action?=?"Index",?id?=?UrlParameter.Optional?}//?参数默认值????
  14. ???????????);??
  15. ??
  16. ????????????routes.MapRoute(??
  17. ????????????????"ActionHtml",?//?action伪静态????
  18. ????????????????"{controller}/{action}.html/{id}",//?带有参数的?URL????
  19. ????????????????new?{?controller?=?"Home",?action?=?"Index",?id?=?UrlParameter.Optional?}//?参数默认值????
  20. ????????????);??
  21. ??
  22. ????????????routes.MapRoute(??
  23. ????????????????"ControllerHtml",?//?controller伪静态????
  24. ????????????????"{controller}.html/{action}/{id}",//?带有参数的?URL????
  25. ????????????????new?{?controller?=?"Home",?action?=?"Index",?id?=?UrlParameter.Optional?}//?参数默认值????
  26. ????????????);??
  27. ????????????routes.MapRoute(??
  28. ???????????????"Root",??
  29. ???????????????"",??
  30. ???????????????new?{?controller?=?"Home",?action?=?"Index",?id?=?UrlParameter.Optional?});//根目录匹配????
  31. ??
  32. ????????????routes.MapRoute(??
  33. ????????????????name:?"Default",??
  34. ????????????????url:?"{controller}/{action}/{id}",??
  35. ????????????????defaults:?new?{?controller?=?"Home",?action?=?"Index",?id?=?UrlParameter.Optional?}??
  36. ????????????);??
  37. ????????}??
  38. ????}??


第四:配置IIS

配置完Web.config,基本就可以直接访问地址了,如果是发布到IIS7还是要进行配置,具体配置查看如下地址:

http://www.cnblogs.com/zhongweiv/archive/2011/10/29/UrlRewriter_IIS.html