Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url.Action in asp.net core

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.

like image 269
pitaridis Avatar asked Dec 12 '17 08:12

pitaridis


1 Answers

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")
like image 56
pitaridis Avatar answered Oct 15 '22 15:10

pitaridis