I have a GET action in a controller and it needs to accept a parameter. This is for an edit view so I pass in the id (which is of type Guid) to the action.
Currently my URL looks like
controller/Edit?ItemId=<Guid>
Is there a way to make the urls look a little prettier, like this?
controller/Edit/<Guid>
My action signature:
public ActionResult Edit(Guid ItemId)
My route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Yes, putting [FromRoute]
in the action signature should do it. I mean
public ActionResult Edit([FromRoute]Guid id)
In this case the id
variable will contain the <Guid>
part controller/Edit/<Guid>
of the URL.
It is important, that the parameter name id
should be the same as the route parameter name {id}
in the mapping.
Another option is to use attribute routing:
[Route("controller/edit/{ItemId}")]
public ActionResult Edit(Guid ItemId)
{ }
And then enable it where you register your routes:
routes.MapMvcAttributeRoutes();
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.