ASP.NET MVC4 User Routing

I want to create a simple blog engine. For trendy and clean URLs, I would like to use the routing mechanism implemented in MVC4.

I added the following lines to RouteConfig.cs:

public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "ArticleList", url: "Articles/{category}/{page}", defaults: new { controller = "Articles", category = UrlParameter.Optional, page = 1 }); } } 

And if I write to the web browser url:

 http://localhost:6666/Articles/SomeCategory/3 

I want to go to this controller:

 public class ArticlesController : ControllerBase<IHomeService> { public ActionResult Index(string category, int page = 0) { return View("~/Views/Article/Articles.cshtml"); } } 

with the parameters category = "SomeCategory" and page = 1.

All I get is a Server Error in the application '/' Resource not found.

What's wrong?

+4
source share
3 answers
  routes.MapRoute( name: "ArticleList", url: "{controller}/{category}/{page}", defaults: new { category = UrlParameter.Optional, page = 1, action = "Index" }, constraints: new { controller = "Articles" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 

EDIT

I should have added this to the answer, but I was in a hurry:

  • Register your custom routes first, all the more common the higher priority.
  • In the above example, using constraints or hard coding, the route gives the same result. The restrictions are more flexible, because you can use a regular expression to limit the values โ€‹โ€‹of the controllers / actions / parameters for which your route is intended. For example, if you add a new route that uses the / category / page template, you can change the controller restriction accordingly:

    restrictions: new {controller = @ "^ (Articles | AnotherController) $"}

+5
source

The problem is that you have an optional parameter in the middle of the {controller} / {category} / {page} path. ASP.NET routing has a problem with this, because if a category is not specified, it has no way to determine that the category is not specified. There is a solution for having optional parameters in the middle of the paths here . It solves a more general problem, but it also covers your problem.

0
source

To enable attribute routing, call MapMvcAttributeRoutes during configuration. Below is the code.

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

In MVC5, we can combine attribute routing with legend-based routing. Below is the code.

  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 } ); } 

It is very easy to make the URI parameter optional by adding a question mark to the route parameter. We can also specify a default value using the form = value parameter. here is the full article.

0
source

Source: https://habr.com/ru/post/1492772/


All Articles