If you use Route
attribute, it will override the route template.
So if you use ASP.NET Core MVC, you need use the route template like below:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Your Controller should be:
public class BookController : Controller
{
[HttpGet]
public ActionResult Update(int id) {
return View();
}
}
Then the Url.Action should work as expected:https://localhost:44345/Book/Update/1
<a onclick="location.href='@Url.Action("Update", "Book", new { id = @Model.Id })'"
class="btn btn-primary">Edit</a>
If you use ASP.NET Core WebApi, controller should be like below:
[Route("[controller]/[action]")]
[ApiController]
public class BookController : ControllerBase
{
[HttpGet]
[Route("{id}")]
public ActionResult Update(int id) => Ok();
}