mvc 路由伪静态实现
程序员文章站
2024-02-12 13:01:46
...
很多网站都采用伪静态,例如以html、shtml等结尾的url,mvc的路由可以轻松实现。
配置路由
默认路由配置
添加伪静态路由
mvc的路由原理是从上往下匹配的,所以只需要在后面添加自己配置的路由即可
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default1",
url: "{controller}/{action}/{id}/{page}.html",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}.html",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default3",
url: "{controller}/{action}/{id}.html",
defaults: 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 }
);
}
修改web.config配置
添加完后并不能够映射路由,需要配置下web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
配置完后即可通过你自己配置的url访问您的网站了