2014-12-30 86 views
3

以下几点有什么区别?属性路由差异?

[Route("movies/genre/{genre}")] 
public ActionResult ViewByGenre(string genre="action") 
在此statment

[Route("movies/genre/{genre}")] 
public ActionResult ViewByGenre(string genre="action") 

genre参数

而不是

[Route("movies/genre/{genre=action}")] 
public ActionResult ViewByGenre(string genre) 
+0

为什么不试试看?你有什么问题使用它的方式之一? – mybirthname

回答

4

不在路线可选,只有ViewByGenre功能将稳定物价它。

这里

[Route("movies/genre/{genre=action}")] 
public ActionResult ViewByGenre(string genre) 

你是说genre参数是在路由可选。如果到达空值,则需要action值。 ViewByGenre函数总是应该有类型参数增值的

的文档

参考here这样你在做属性的路由。其优点是,通过属性路由,您可以更多地控制应用程序中的URI,当您在代码中编辑某些内容时不会损坏其他路线。 在另一边的公约基地规则的一个例子是,当你决定你在RouteConfig.cs规则文件这样

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapMvcAttributeRoutes(); 

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

好的,我明白了。我还有其他几个问题。 – FaizanRabbani

+0

如果您需要帮助我在这里 – faby

+0

那么'属性路由'和基于会议的路由之间的主要区别是什么?我应该去哪一个? – FaizanRabbani