Question

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)

Was it helpful?

Solution

This is an even better solution.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top