开发者

ASP.NET MVC - What's the best way to create a Url to controller action from within view?

I need to figure out the best way to create a url to a controller action from within a view. The problem I am running into is when I deploy the application to different machines with and without a virtual application root the url changes slightly.

So I want to be able to construct the url dynamically and the method needs to be aware of whether or not the application is running in an application root or if it resides at the root.

So what helpers/classes should I be leveraging to get this done?

<%=ResolveUrl(string.Concat(Request.Url.Authority,Reque开发者_运维知识库st.ApplicationPath) + "/Session/GetSessionFile/" + Model.SessionID + "/" + Model.FileName)%>


You should be using Url.Action. You pass the action as one of the paramters. Then a series of route values for the others. I'm guessing what those route value keys are (id and fileName):

<%= Url.Action("GetSessionFile", new { id = Model.SessionId, fileName = Model.FileName })


You can use Url.Action() helper:

<%=Url.Action("GetSessionFile", "Session") + '/'  Model.SessionID + '/' + Model.FileName %>

UPDATE:

I'll try to guess your routing configuration. If it contains the following statement:

routes.MapRoute("Session_Route", "{controller}/{action}/{sessionId}/{arg}");

you surely should pass the parameters that way:

<%=Url.Action("GetSessionFile", "Session", new { sessionId = Model.SessionID, arg = Model.FileName}) %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜