[英]Asp.net MVC 5 Attribute Routing Constraint
Looking at the help Attribute Routing in ASP.NET MVC 5 it is easy to see how to constraint a parameter as below: 查看ASP.NET MVC 5中的帮助属性路由,很容易看到如何约束参数,如下所示:
[Route("edit/{promoId:int?}")]
public ActionResult Edit(int? promoId) { … }
So this route will only accept promoId
with int values or empty. 因此,此路由仅接受具有int值或为空的promoId
。
Some valid URLs for this route would be: 此路线的一些有效URL为:
/promotions/edit/5
/promotions/edit/
But how to set a RouteAttribute
to accept "/promotions/edit/promoId=5"? 但是,如何设置RouteAttribute
接受“ / promotions / edit / promoId = 5”?
Actually, I think the url should be in this format: 实际上,我认为网址应采用以下格式:
/promotions/edit?promoId=5
Note the ?
注意?
. 。 It's the beginning of the query string marker. 这是查询字符串标记的开始。
It should be possible to do it this way: 应该可以这样做:
[Route("edit")]
public ActionResult Edit([FromUri]int promoId)
{
...
}
You try to set it in RouteConfig.cs
in your App_Start
folder by pointing that kind of URL to your action. 您尝试通过将此类URL指向您的操作来在App_Start
文件夹的RouteConfig.cs
中进行设置。
routes.MapRoute(
name: "Edit",
url: "{controller}/{action}/promoId={promoId}",
defaults: new { controller = "Promotions", action = "Edit", promoId = UrlParameter.Optional }
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.