7

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.

1 Answer 1

24

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")
Sign up to request clarification or add additional context in comments.

1 Comment

@Url.Action("Index", "Home") should do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.