Reputation: 3439
What is the difference between the following?
[Route("movies/genre/{genre}")]
public ActionResult ViewByGenre(string genre="action")
Instead of
[Route("movies/genre/{genre=action}")]
public ActionResult ViewByGenre(string genre)
Upvotes: 3
Views: 210
Reputation: 7556
in this statment
[Route("movies/genre/{genre}")]
public ActionResult ViewByGenre(string genre="action")
genre
parameter isn't optional in the route, only ViewByGenre
function will valorize it.
here
[Route("movies/genre/{genre=action}")]
public ActionResult ViewByGenre(string genre)
you are saying that genre
parameter is optional in the route. If it arrives null it will take action
value. ViewByGenre function always should have genre parameter valorized
refer here for documentation
In this way you are doing attribute routing. The advantages are that Attribute Routing gives you more control over the URI in the applications, when you edit something in the code it won't break another route. On the other side an example of Convention base rules is when you decide your rules in the RouteConfig.cs file like this
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 }
);
}
Upvotes: 4