找到与请求的URL匹配的多种控制器类型

编程入门 行业动态 更新时间:2024-10-16 22:15:10
本文介绍了找到与请求的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(); } }

,因为destinationId和productId的类型为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匹配的多种控制器类型

本文发布于:2023-11-02 17:02:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   多种   类型   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!