Url.Action in asp.net core

╄→гoц情女王★ 提交于 2019-12-11 16:20:48

问题


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:


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")


来源:https://stackoverflow.com/questions/47767851/url-action-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!