2014-07-14 67 views
0

我正試圖縮短新MVC 5項目中的About和Contact Urls。我嘗試過的所有東西都不起作用。縮短MVC中的關於Url 5

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 
+0

你能發佈RegisterRoutes()完整的代碼嗎? –

回答

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

你可以這樣做(C#)。

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

並確保它在默認路由之前。