简体   繁体   中英

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:

[Route("edit/{promoId:int?}")]
public ActionResult Edit(int? promoId) { … } 

So this route will only accept promoId with int values or empty.

Some valid URLs for this route would be:

/promotions/edit/5
/promotions/edit/

But how to set a RouteAttribute to accept "/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.

routes.MapRoute(
           name: "Edit",
           url: "{controller}/{action}/promoId={promoId}",
           defaults: new { controller = "Promotions", action = "Edit", promoId = UrlParameter.Optional }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM