1

I have enable attribute routing in route config file and I have declare attribute routing as

[RoutePrefix("receive-offer")]
public class ReceiveOfferController : Controller
{
    // GET: ReceiveOffer
    [Route("{destination}-{destinationId}")]
    public ActionResult Index(int destinationId)
    {
        return View();
    }
}


public class DestinationController : Controller
{
    [Route("{country}/{product}-{productId}")]
    public ActionResult Destination(string country, string product, int productId)
    {
        return View();
    }

}

In above two controllers one has static prifix and other has variable prefix but I am getting Multiple controller types were found that match the URL error from these two controller.

What is wrong in this routing pattern.

Bhuban Shrestha
  • 1,304
  • 11
  • 27

1 Answers1

1

this happens when the attribute route matches multiple route you can look at this Multiple controller types were found that match the URL. so when you enter domain/receive-offer/new york-1 it matches the first Route and also the second URL because it will consider receive-offer as a country so to resolve this we can use Route Constraints to specify the values of routes so your route will be

 [RoutePrefix("receive-offer")]
    public class ReceiveOfferController : Controller
    {
        // GET: ReceiveOffer
        [Route("{destination}-{destinationId:int}")]
        public ActionResult Index(int destinationId)
        {
            return View();
        }
    }


    public class DestinationController : Controller
    {
        [Route("{country:alpha}/{product}-{productId:int}")]
        public ActionResult Destination(string country, string product, int productId)
        {
            return View();
        }
     }

since destinationId and productId will be of type int and country will be alphabet but keep in mind that if you add spaces in country name the route will not work so you will have to apply regax or you can remove spaces between the country name like HongKong

Community
  • 1
  • 1
Usman
  • 4,615
  • 2
  • 17
  • 33
  • Could you please justify the purpose of :alpha after country in DestinationController route. Or any reference that I can look for – Bhuban Shrestha Apr 24 '17 at 16:01
  • @Bhuban its called routing constrain means specify the type of the value in route and here alpha means alphabet characters (a-z, A-Z). you can read [attribute-routing](https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/) section Route Constraints – Usman Apr 24 '17 at 16:10
  • the reason why i used it to make route a bit unique because `receive-offer` is a string and matches route `{country}` so by adding alpha `{country:alpha}` means it will not contain any special character like `-` – Usman Apr 24 '17 at 16:13
  • Thank you @Usman my above requirement is fulfilled but now I got another issue. In a controller where I don't want attribute routing and want to execute index action just by domain/controller it is executing DestinationController index actions since domain/{country} matched with domain/{controller} how should I handle this one. – Bhuban Shrestha Apr 24 '17 at 17:39
  • @Bhuban you can name index `[Route("Destination")]` – Usman Apr 24 '17 at 17:49
  • sorry I didn't understand properly. Where should I put that route in? – Bhuban Shrestha Apr 24 '17 at 17:54
  • above the index action of DestinationController – Usman Apr 24 '17 at 17:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142531/discussion-between-bhuban-and-usman). – Bhuban Shrestha Apr 24 '17 at 17:57