.net Core - the default controller does not load when using the Route attribute

A new .net web application project with the following route configuration:

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

If you replace this with app.UseMvc()and add the appropriate attributes Routefor the HomeController and its actions (Index, About, Contact, Error), it will still work. Since we do not specify a default route, the default view (Home / Index) will not be displayed if you click http: // localhost: 25137 / . Hope the understanding is correct!

Now, since I need the default view to appear when I click http: // localhost: 25137 / , I changed the routing code to app.UseMvcWithDefaultRoute();which by definition it will execute the equivalent of the original fragment. Even then, he did not show the default view; but worked when using the full url ( http: // localhost: 25137 / home / index ). This means that routing still works, but not by default!

Then I went back to the controller and removed all the attribute Routefrom the HomeController and its actions. Then the default routing worked without any problems.

Is this expected behavior? What could be causing this behavior?

+4
1

asp docs:

, . . , , . .

, , , , UseMvc UseMvcWithDefaultRoute, . .

, , . asp docs:

public class MyDemoController : Controller
{
   [Route("")]
   [Route("Home")]
   [Route("Home/Index")]
   public IActionResult MyIndex()
   {
      return View("Index");
   }
   [Route("Home/About")]
   public IActionResult MyAbout()
   {
      return View("About");
   }
   [Route("Home/Contact")]
   public IActionResult MyContact()
   {
      return View("Contact");
   }
}
+12

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


All Articles