1

是否有一种使用 ASP.NET MVC3 在页面上显示 URL 的优雅方式。我正在寻找的方法应该接受 contollerName、actionName 和 routeValue(类似于 @Html.ActionLink() 通过参数)

egCode: 这是链接:@thatMethod ("Show", "Post", new { Post = 0 })

egResult:这是链接:http://localhost:3120/Post/Show/0

编辑1: 我不希望它是超链接,只是纯文本。

编辑 2: 它需要显示域(在前面的示例中为“http://localhost:3120”)

4

2 回答 2

0

是一个更好的解决方案。

于 2011-08-31T13:28:09.993 回答
0

您可以使用 Html Helper 扩展。

Html 助手类

public static class HtmlHelpersExtensions
{
   public static string ActionLinkText(this HtmlHelper helper, string linkText, string actionName, string controllerName,object htmlAttributes, string spanAttributes)
  {
     TagBuilder spanBuilder = new TagBuilder("span");
     spanBuilder.InnerHtml = linkText;
     spanBuilder.Attributes.Add("class", spanAttributes);

     return BuildAnchor(spanBuilder.ToString(), string.Format("/{0}/{1}", controllerName, actionName), htmlAttributes);
  }

  private static string BuildAnchor(string innerHtml, string url, object htmlAttributes)
  {
     TagBuilder anchorBuilder = new TagBuilder("a");
     anchorBuilder.Attributes.Add("href", url);
     anchorBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
     anchorBuilder.InnerHtml = innerHtml;

     return anchorBuilder.ToString();
   }
}

看法

@Html.ActionLinkText("Your Text", "Show", "Post", new { @title = "Your Text" }, "").Raw()

这可能会对您有所帮助。

于 2011-08-31T13:57:13.150 回答