2017-04-24 78 views
0

我有使屬性在路由配置文件路由和我聲明屬性路由作爲多個控制器類型匹配所請求的URL的是

[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錯誤相匹配。

這種路由模式有什麼問題。

+0

可以顯示網址? – Usman

+0

URL就會像 (域/接收報價/紐約-1)/////////// (域/ USA /紐約-1) 在上述兩個URL USA可以是取而代之的是任何其他國家的收貨報價是靜態的。 –

回答

0

發生這種情況時,屬性路線匹配多條路線,你可以看看這Multiple controller types were found that match the URL。所以,當你進入domain/receive-offer/new york-1它匹配的第一個幹線,也是第二個URL,因爲它會考慮receive-offer作爲一個國家,所以要解決這一點,我們可以使用Route Constraints 指定路線的數值,以便您的路線將是

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

destinationIdproductIdint型和countryalphabet但請記住,如果你加在國名的路線不會那麼的工作空間,你將不得不申請regax,也可以刪除國名之間的空間,如HongKong

+0

您能否證明DestinationController路由中國家的alpha值:或者我可以查找的任何參考 –

+0

@Bhuban其稱爲路由約束是指指定路由中值的類型,這裏的alpha表示字母字符(a-z,A-Z)。你可以閱讀[屬性路由](https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/)部分路由約束 – Usman

+0

原因爲什麼我使用它來使路由有點獨特,因爲'receive-offer'是一個字符串,並匹配路由'{country}',因此通過添加alpha'{country:alpha}'意味着它不會包含任何特殊字符,例如'-' – Usman

相關問題