1

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)

Miro
  • 1,778
  • 6
  • 24
  • 42

2 Answers2

0

This is an even better solution.

Community
  • 1
  • 1
Major Byte
  • 4,101
  • 3
  • 26
  • 33
  • No I don't want it as hyperlink,forgot to mention.Thx for help – Miro Aug 31 '11 at 13:32
  • I tried just as you told, and i got "/Post/Show/0" as a result,but I wanted to get "http://localhost:3120/Post/Show/0" .Any idea? – Miro Aug 31 '11 at 13:41
  • Hmm I realized that, in the mean while I found [this](http://stackoverflow.com/questions/434604/how-to-i-find-the-absolute-url-of-an-action-in-asp-net-mvc) what just does what you want, if you add routeValues as a parameter – Major Byte Aug 31 '11 at 13:43
  • Can you post that last one as a new answer so I could accept it as an Answer? Great,Thx :) – Miro Aug 31 '11 at 13:56
0

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.

getmk
  • 155
  • 3
  • 13