Possible Duplicate:
How to I find the absolute url of an action in ASP.NET MVC?
Asp MVC Action link absolute url
你好,
有没有一种简单的方法可以为特定操作生成完整的 URL?我试过这个:
urlHelper.Action("Action", "Controller")
但这确实会生成一个“\”。
致以最诚挚的问候
编辑 1: 我试过这个:
urlHelper.Action("List", "Ad", null, "http");
但它只返回:localhost:16055。我期待像 localhost:16055/Ad/List 这样的东西?
使用 Html.ActionLink("Link Title", "Action", "Controller")
要生成完整链接,请使用:
@Html.ActionLink("Link Title", "Action", "Controller", "http", "www.mysampledomain.com",
"_blank", new {id = paramValue}, new { @class="someClass" })
这是带有您可以指定的所有参数的扩展重载。看看这篇 MSDN 文章 http://msdn.microsoft.com/en-us/library/dd492938.aspx
要从 Controller 生成,请使用此代码:
var url = UrlHelper.GenerateUrl(null, "MyAction", "MyController", "http", "www.mydomain.com", String.Empty, null, RouteTable.Routes, this.ControllerContext.RequestContext, false);
url 变量将包含您的 URL 的字符串表示形式。您可以像这样将其存储在 ViewBag 中:
ViewBag.MyUrl = UrlHelper.GenerateUrl(null, "MyAction", "MyController", "http", "www.mydomain.com", String.Empty, null, RouteTable.Routes,
this.ControllerContext.RequestContext, false);
从 View 中称它为:
@ViewBag.MyUrl
应该是这样吧。