Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing default action doesn't work for ASP.NET Core 2

On latest ASP.NET MVC CORE 2 default template, i am trying to change default action by modifying controller and action as below. I am expecting to see login page as default but i am having 404 http error. What I am doing wrong ?

You can verify this issue on default ASP.NET CORE 2 MVC project.

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Account}/{action=Login}/{id?}");
    });
}
like image 645
Freshblood Avatar asked Jul 20 '26 00:07

Freshblood


1 Answers

If you look at the AccountController class, you'll see it's decorated with a Route attribute, like so:

[Route("[controller]/[action]")]
public class AccountController : Controller

However, if you look at the HomeController class, you'll see that it is not decorated with such an attribute:

public class HomeController : Controller

Because the AccountController is using Attribute routing, it will not be picked up using the Conventional routing template. The docs explain this mutual exclusivity:

Actions that define attribute routes cannot be reached through the conventional routes and vice-versa.

like image 62
Kirk Larkin Avatar answered Jul 22 '26 15:07

Kirk Larkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!