I like the new tag helpers attributes which make readable dynamic html contents. I can create a link by using the asp-controller and asp-action attributes like this:
<a asp-controller="Home" asp-action="Index">Index</a>
The problem is that there are cases where I need just the URL of the action in order to use it my javascript. For example in order to make an ajax call in ASP.NET MVC 5, I could have the following code:
$.ajax({
type: "POST",
url: '@Url.Action("GetData","Ajax")',
data: "{ }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
}
});
I tried to find an equivalent method in ASP.NET Core but I was not able to find one so I have to create the URL myself. Is there a way to get the Action and Razor Page URL in ASP.NET Core.
I just found how to do it. In case someone else is looking for the same thing here is how to do it.
@this.Url.Action("Index", "Home")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With