Dot NET core 3.1 passing parameter to action with MapControllerRoute

Viewed 639

I am trying to achieve a particular url patterns that I find clean and readable Am trying to construct a url like this

/students/updatestudent/29/20c52772-9362-470a-aed1-87a92fd28a11/

I want t capture the student ID and the student UUID and pass them to the action. Here is how the action looks like.

 public IActionResult UpdateStudent(int Id, string uuid){

 }

Here are my MapControllerRoute

endpoints.MapControllerRoute(
      name:"studentsupdate",
      pattern: "{controller=Students}/{action=UpdateStudent}/{Id}/{uuid}/",
      defaults: new { controller = "Students", action = "UpdateStudent", Id = "", uuid = "" }
 );

When I try the following;

<a href='@Url.Action("UpdateStudent", "studentsupdate", new { Id = student.Id, uuid=student.UniqueUUID })/' class="a--color2">

I get the following;

https://localhost:44361/Students/UpdateStudent/29?uuid=20c52772-9362-470a-aed1-87a92fd28a11/

Instead of

https://localhost:44361/Students/UpdateStudent/29/20c52772-9362-470a-aed1-87a92fd28a11/
1 Answers

Usually when I want a specific route for a method, I decorate the method with a [Route()] attribute. Here it would be [Route("UpdateStudent/{id}/{uuid}")] just above your UpdateStudent method.

And then reach the endpoint using just the controller name and action name, not a route name.

<a asp-contoller="Students" asp-action="UpdateStudent" asp-route-id="21" asp-route-uuid="someUuid">

Related