设置主页的路由

时间:2014-02-26 22:54:56

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我的RouteConfig.cs文件中有两条路线。我不能同时运行这两个,所以顶部的那个被执行:

routes.MapRoute(
        "ScNewsList",
        "{controller}/{id}/{title}",
        new { controller = "news", action = "SpecialCollectionList", id = UrlParameter.Optional, title = UrlParameter.Optional }
        );

        routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional }
        );

我如何为两者运行规则:

http://localhost:53098/news/312/SpecialCollectionList
http://localhost:53098/

第二个应该映射到主页?

2 个答案:

答案 0 :(得分:1)

如果您想要始终对新闻控制器运行ScNewsList路线,那么您可以将网址更新为"news/{id}/{title}"

这样,Default路线将成为ScNewsList路线之外的所有路线。

答案 1 :(得分:0)

你可以通过手动设置路线到新闻

这样做
routes.MapRoute(
    "ScNewsList",
    "news/{id}/{title}",
    new { controller = "news", action = "SpecialCollectionList", id =     UrlParameter.Optional, title = UrlParameter.Optional }
    );

    routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional }
    );