开发者

ASP.NET MVC: Proper way to consume/link to a resource from another controller

开发者 https://www.devze.com 2022-12-22 01:54 出处:网络
In a view, I want to consume some Json produced by an action on another controller. I don\'t think I ought to be arbitrarily specifing the url for the resource because if I say something like \"/Deale

In a view, I want to consume some Json produced by an action on another controller. I don't think I ought to be arbitrarily specifing the url for the resource because if I say something like "/Dealerships/GetDealerData" and if my application is a virtual directory at say "www.somesite.co开发者_如何学编程m/MyApplication", then my reference to that resource would break.

I'm sure there's something to do w/ Routes that will allow me to correctly generate the url of the resource by specifing the controller and action name but I don't know what it is and am having difficulty finding it. Can someone please point me in the right direction?


You can use Url.Action(). Something like this:

<%=Url.Action("MyAction", "MyController")%>

This is the same as <%=Html.ActionLink("MyAction", "MyController")%> but only generating the url, not the <a /> tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号