9

Possible Duplicate:
How to I find the absolute url of an action in ASP.NET MVC?
Asp MVC Action link absolute url

Hi,

Is there a easy way to generate the full URL for a specific action? I have tried this :

urlHelper.Action("Action", "Controller")

But this does generates a "\".

BestRegards

Edit 1: I have tried this :

urlHelper.Action("List", "Ad", null, "http"); 

but it only returns : localhost:16055. I was expecting somthing like localhost:16055/Ad/List ?

Community
  • 1
  • 1
Banshee
  • 15,376
  • 38
  • 128
  • 219

2 Answers2

20

Use Html.ActionLink("Link Title", "Action", "Controller")

To generate a full link use:

@Html.ActionLink("Link Title", "Action", "Controller", "http", "www.mysampledomain.com", 
                 "_blank", new {id = paramValue}, new { @class="someClass" })

That is the extension overload with all the parameters you can specify. Have a look at this MSDN article http://msdn.microsoft.com/en-us/library/dd492938.aspx

To generate from Controller use this code:

var url = UrlHelper.GenerateUrl(null, "MyAction", "MyController", "http", "www.mydomain.com", String.Empty, null, RouteTable.Routes, this.ControllerContext.RequestContext, false);

url variable will contain a string representation of your URL. You can store it in ViewBag like:

ViewBag.MyUrl = UrlHelper.GenerateUrl(null, "MyAction", "MyController", "http", "www.mydomain.com", String.Empty, null, RouteTable.Routes,
                                        this.ControllerContext.RequestContext, false);

From View call it as:

@ViewBag.MyUrl

That should be it.

Huske
  • 9,186
  • 2
  • 36
  • 53
  • How do I do this from a controller action? – Banshee Apr 24 '12 at 15:27
  • 2
    Html.ActionLink generates a link. If you want the full URL, use the Url helper, Url.Action. One of the overloads on that method allows you to specify the Schema and in doing so will generate a fully qualified URL, like http://localhost:1234/controller/action – Nick Bork Apr 24 '12 at 15:29
  • Not necessarily. Html.ActionLink has an overload that can create a full link as well. – Huske Apr 24 '12 at 15:32
  • What are you going to do with the HTML code for a "link" in a controller? The difference is Html.ActionLink generates HTML for Text and Url.Action generates a string of the URL – Nick Bork Apr 24 '12 at 15:37
  • @NickBork, I am not sure I understand your question. – Huske Apr 24 '12 at 15:38
  • @Huske , SnowJim asks "How do I do this from a controller action" to which my answer is why would you want the raw HTML of the Html.ActionLink? If you're in the controller then chances are you want Url.Action to get the string value of the URL and not the HTML string of the full link. Now, if you're in a view there are cases where you'll want to be able to use both Html.ActionLink to get the raw HTML as well as Url.Action to generate a URL. – Nick Bork Apr 24 '12 at 15:59
  • @NickBork, I haven't seen that part of the question and I cannot see it now. It seems the question was updated a few times. – Huske Apr 24 '12 at 18:28
  • Right below your answer, comment from SnowJim: "How do I do this from a controller action?" – Nick Bork Apr 24 '12 at 18:35
  • Good point @NickBork :-). I will update my answer. – Huske Apr 24 '12 at 18:38
0

Url.action generates an url relative. To generate an url absolute you can look this: How do I find the absolute url of an action in ASP.NET MVC?

Community
  • 1
  • 1
Diego Dias
  • 904
  • 3
  • 15
  • 23
  • I have tried this : urlHelper.Action("List", "Ad", null, "http"); but it only returns : http://localhost:16055/. I was expecting somthing like http://localhost:16055/Ad/List ?? – Banshee Apr 24 '12 at 16:32