开发者

An elegant way of showing URL on page

Is there an elegant way of showing URL on page using ASP.NET MVC3 .The method I'm looking for should accept contollerName,actionName and routeValue(similiar to @Html.ActionLink() by arguments)

e.g.Code: Here's the link: @thatMethod("Show",开发者_如何学编程 "Post", new { Post = 0 })

e.g.Result: Here's the link: http://localhost:3120/Post/Show/0

EDIT 1: I dont wan't it to be hyperlink,just plain text.

EDIT 2: It needs to show domain ("http://localhost:3120" in previous example)


This is an even better solution.


You can use Html Helper extensions.

Html Helper class

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();
   }
}

View

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

This may help you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜