Improve routes

0

People have some better way to do these routes? Or for every action in my Controller, will I have to create your routing?

app.UseMvc(routes =>
        {
            routes.MapRoute(
               "Login",
               "Login",
               new { controller = "Usuario", action = "Login" }
               );
            routes.MapRoute(
               "registro",
               "Registro",
               new { controller = "Usuario", action = "Registro" }
               );
    
asked by anonymous 19.09.2018 / 19:44

1 answer

1

When creating an ASP.NET Core MVC project, you will default to the following route:

app.UseMvc(routes =>
{
   routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

Basically, it interprets as follows:

  • 1st Token: Controller : If not informed, use the controller Home ;
  • 2nd Token: Action : If you are not informed, use the Index method;
  • 3rd Token: ID : If not informed, it will not be sent (optional). And it will only be sent if the method informed, in the 2nd token, has some parameter that is called "id";

This route template can serve the vast majority of your site's routes. You can even expand or add new generic routes.

If all of your routes are similar, you can only create one using tokens tokenizing values that can be passed on Route.

By analyzing the default route, the following routes will be supplied:

  

/ Products / Details / 5

Interpreted as follows:

{ controller = Products, action = Details, id = 5 }

Or just

  

/ Products

Interpreted as follows:

{ controller = Products, action = Index}

More information can be found in the Routing to controller actions manual. in ASP.NET Core

    
19.09.2018 / 20:01