找到与请求的URL匹配的多种控制器类型 [英] Multiple controller types were found that match the Requested URL

查看:156
本文介绍了找到与请求的URL匹配的多种控制器类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在路由配置文件中启用了属性路由,并且将属性路由声明为

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();
    }

}

在以上两个控制器中,一个具有静态prifix,另一个具有可变前缀 但我发现找到了与这两个控制器的URL错误匹配的多个控制器类型.

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.

推荐答案

这种情况发生在属性路由与多个路由匹配时,您可以查看此找到了与URL匹配的多种控制器类型.因此,当您输入domain/receive-offer/new york-1时,它会匹配第一个路线和第二个URL,因为它将把receive-offer视为一个国家/地区,因此要解决此问题,我们可以使用

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();
        }
     }

,因为destinationIdproductId的类型为int,而country的类型为alphabet,但是请记住,如果在国家/地区名称中添加空格,则该路线将无法使用,因此您必须申请regax,或者您可以删除国家名称之间的空格,例如HongKong

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

这篇关于找到与请求的URL匹配的多种控制器类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆