缩短MVC 5中的关于Url

时间:2014-07-14 10:45:47

标签: asp.net vb.net asp.net-mvc-5 asp.net-mvc-routing

我正在尝试缩短新MVC 5项目中的About和Contact Urls。我尝试的一切都行不通。

  routes.MapRoute("ShortAbout", "About", New With {.controller = "Home", .action = "About"})


  routes.MapRoute("ShortenUrl", "{action}", New With {.controller = "Home", .action =       `"Index", .id = UrlParameter.Optional}, New With {.action = "About|Contact"})`

我错过了什么?我知道这必须简单。我想得到一个像http://mysite/About而不是/ home / About的网址。

   Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

    routes.MapRoute("ShortAbout", "About", New With {.controller = "Home", .action = "About"})


    routes.MapRoute("ShortenUrl", "{action}", New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}, New With {.action = "About|Contact"})


End Sub

2 个答案:

答案 0 :(得分:1)

尝试更改RegisterRoutes()方法,如下所示;

Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

    routes.MapRoute(
        "ContactRoute",
        "Contact",
        New With {.controller = "Home", .action = "Contact"}
    )

    routes.MapRoute(
        "AboutRoute",
        "About",
        New With {.controller = "Home", .action = "About"}
    )

    routes.MapRoute( _
        name:="Default", _
        url:="{controller}/{action}/{id}", _
        defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
    )
End Sub

谢谢!

答案 1 :(得分:1)

你可以这样做(C#)。

routes.MapRoute(
            name: "About",
            url: "About",
            defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
        );

并确保它在默认路线之前。