MVC Route conflict seems strange

user1376198

I am using MVC 5.1 with AutoFac.

I dont understand why the below route from each controller conflict with this URL: https://localhost:44300/Home/login

I thought it would map to the first method. I get this error though:

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: AllThings.WebUI.Controllers.AccountController AllThings.WebUI.Controllers.PostController


    public class AccountController : Controller
{
    //
    // GET: /Account/Login
    [Route("~/{site}/Login")]
    [Route("~/Account/Login")]
    [Route("~/{country:maxlength(2)}/{site}/Login")]
    [Route("~/{country:maxlength(2)}/Account/Login")]
    [AllowAnonymous]
    public ActionResult Login(string returnUrl, string country, string site)
    {
        return View();
    }
}


    public class PostController : Controller
{
    [Route("~/{site}/{CategoryUrl?}")]
    [Route("~/{country:maxlength(2)}/{site}/{CategoryUrl?}", Name = "ResultList")]
    [AllowAnonymous]
    public ActionResult List(string country, string site, SearchCriteriaViewModel searchCriteriaViewModel)
    {
        return View("List", searchCriteriaViewModel);
    }
}
NightOwl888

The main problem is that you have 3 possible routes that can match /Home/Login.

[Route("~/{site}/Login")]
[Route("~/Account/Login")]
[Route("~/{site}/{CategoryUrl?}")]

Liberal use of placeholders, especially that is all you have in a URL template definition is not a good thing. You should use literals in the URL or if you use placeholders, there should be constraints on them so they don't conflict.

Note that the following routs conflict as well:

[Route("~/{country:maxlength(2)}/{site}/Login")]
[Route("~/{country:maxlength(2)}/Account/Login")]
[Route("~/{country:maxlength(2)}/{site}/{CategoryUrl?}", Name = "ResultList")]

Any one of them could match UK/Account/Login.

Additionally, using a tilde (~) is to override a route prefix (see the MSDN documentation). If your controller doesn't define one, you should just start with the first segment or placeholder.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related