2

Using Attribute Routing (Route Attribute), I have two action methods with same name in two different controllers as follows

public class HomeController : Controller
{
//URL: /accessMethod
    [Route(“accessMethod”)]
    public ActionResult Method1()
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        return View();
    }
}

public class AccountController : Controller
{
//URL: /accessMethod
    [Route(“accessMethod”)]
    public ActionResult Method2()
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        return View();
    }
}

while hitting the url http://localhost:1234/accessmethod, the following error occurred

Server Error in '/' Application.

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

The request has found the following matching controller types: mvc_Test.Controllers.Page1Controller mvc_Test.Controllers.Page2Controller

Requesting you, How to resolve this issue without using RoutePrefix Attribute? Instead RoutePrefix, we can use Controller name right? Then what is the main use of Attribute Routing?

Brillian
  • 1,411
  • 2
  • 16
  • 23
  • You have a route conflict as both map to the same path. routes have to be uniquely identifiable. – Nkosi Jan 03 '18 at 12:44
  • This appears to be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the ultimate goal you are trying to achieve? – Nkosi Jan 03 '18 at 12:46
  • Consider using a route prefix – Nkosi Jan 03 '18 at 12:48
  • Thanks for your replies. How to resolve this issue without using RoutePrefix Attribute? Instead RoutePrefix, we can use Controller name right? Then what is the main use of Attribute Routing? – Brillian Jan 04 '18 at 05:16
  • Reference [Attribute Routing in ASP.NET MVC 5](https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/) – Nkosi Jan 04 '18 at 10:52

1 Answers1

0

You can add RoutePrefix to controllers:

[RoutePrefix("Account")]
public class AccountController : Controller
{ ...

Then you can access the account one as http://localhost:1234/account/accessmethod. Leaving the other one as it is

Update: Having the below map in your register route map should work for http://localhost:1234/account/accessmethod and http://localhost:1234/home/accessmethod

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id =  UrlParameter.Optional 
});
Tony Thomas
  • 398
  • 7
  • 20
  • Thank you for your reply, How to resolve this issue without using RoutePrefix Attribute? Instead RoutePrefix, we can use Controller name right? Then what is the main use of Attribute Routing? – Brillian Jan 04 '18 at 05:17
  • You can of course manage it through convention routing. But as you come up with more and different patterns of routing you will end up registering more route maps probably in your Global.asax as explained here: https://www.exceptionnotfound.net/attribute-routing-vs-convention-routing/. Attribute routing is an alternative to that – Tony Thomas Jan 04 '18 at 05:35