I am writing an application with the ability to register and enter through a regular HTML form.

On the main page of the application there will be corresponding buttons, I want the user to click on them at xxx.com/registration and xxx.com/login respectively, where the new form with fields, buttons will be friends, everything.

But there is a problem, in order to access the method that returns this same form, you need to write a URL of this type: xxx.com/registration/registration or xxx.com/login/login , that is, contact the controller, and then the method .

QUESTION: How to make it so that you do not need to specify the name of the method in the URL, thereby writing such ugly ways xxx.com/registration/registration , how to make it so that you only need to contact the controller xxx.com/registration , and then loaded right shape?

  • one
    Register routes for these controllers in which the action methods have the correct name by default. Or call these methods Index . - Igor
  • Do not throw sample code? I am now reading an article on metanit - but I can not understand anything - Once Two

1 answer 1

The first decision is to make an action with the name Index in your controller, in this case you can site.ru/controllerName it in the path, and the site.ru/controllerName path will automatically go to the Index action.

This behavior is written in the default route, usually in the startup.cs file.

 routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); 

The second solution is to create a special route for this.

 app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); routes.MapRoute( name: "myRoute", /* шаблон маршрута */ template: "registration"; /* куда отправим запрос по этому шаблону */ defaults: new { controller = "registration", action = "myRegistrationAction" }); }); 

Basics of routing in ASP.NET Core.

The third option is to set routes using attributes:

 [Route("registration")] public IActionResult AnyActionName() 

Routing attributes.

  • one
    and the first solution will work without additional settings? It was then worth already saying about routing with the help of attributes - Grundy
  • @Grundy thanks, done - Dmitry Polyanin 8:51 pm
  • The 3rd option worked perfectly, thanks. - One Two