0
votes

I'm using attribute routing in my MVC5 application and it is working fine. When I tried to create an area and placed attribute routing on the controller inside it, it returns 404.

I know, to enable attribute routing inside Area, I have to use [RouteArea("Area Name Here")] and also have to add routes.MapMvcAttributeRoutes(); inside my RouteConfig class. I did all this and designed my controller like this:

[RouteArea("Client")]
[RoutePrefix("Client")]
public class ClientController : Controller
{
    #region Properties
    private readonly string apiUrl = ConfigurationManager.AppSettings["apiUrl"];
    #endregion

    #region Constructor
    public ClientController()
    {

    }
    #endregion

    #region Action Methods
    [HttpGet, Route("create")]
    public ActionResult Index()
    {
       --logic here
    }
    #endregion
 }

When i run the application using the route: http://localhost:26189/Client/create, I'm able to hit the constructor but not the Index method. Interestingly, if i remove the attribute [HttpGet, Route("create")] and try this route http://localhost:26189/Client/Index it will hit the Index method.

I'm going through these links but not found the exact fix: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/#route-areas

1
I guess you also need ActionName attribute like [ActionName("create")] for index action.Amit
@Amit - I have tried to add thi attribute but no luck :(iSahilSharma
Is there routing definition elsewhere which is overriding yours?Amit
what does the routearea do?, seems like the call would become "/Client/Client/create"?Martea

1 Answers

0
votes

You likely have ordering problems in your startup path. Make sure things are called in this order:

  1. AreaRegistration.RegisterAllAreas();
  2. routes.MapMvcAttributeRoutes();
  3. Any convention-based routes