Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate full URL in MVC? [duplicate]

Tags:

c#

asp.net-mvc

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 ?

like image 938
Banshee Avatar asked Apr 24 '12 15:04

Banshee


1 Answers

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.

like image 154
Husein Roncevic Avatar answered Nov 13 '22 08:11

Husein Roncevic