Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Area in Url.Action?

The problem in Html.ActionLink() is that you can't add additional html content inside the tag that it generates. For example, if you want to add an icon besides the text like:

<a href="/Admin/Users"><i class="fa fa-users"></i> Go to Users</a> 

Using Html.ActionLink(), you can only generate:

<a href="/Admin/Users">Go to Users</a> 

So, to resolve this, you can use Url.Action() to generate only the URL inside the tag like:

// Here, Url.Action could not generate the URL "/admin/users". So this doesn't work. <a href="@Url.Action("", "Users", "Admin")"><i class="fa fa-usesr"></i> Go to Users</a>  // This works, as we know it but won't pass the Area needed. <a href="@Url.Action("", "Users")"><i class="fa fa-users"></i> Go to Users</a> 

So, how do you pass the Area using Url.Action()?

like image 693
doncadavona Avatar asked May 20 '15 05:05

doncadavona


People also ask

How do you add an area to URL action?

You can use this Url. Action("actionName", "controllerName", new { Area = "areaName" }); Also don't forget to add the namespace of the controller to avoid a conflict between the admin area controller names and the site controller names.

What does URL action do?

Generates a fully qualified URL to an action method for the specified action name and route values. Generates a fully qualified URL to an action method by using the specified action name, controller name, and route values.

What is the difference between URL action and HTML action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


1 Answers

You can use this Url.Action("actionName", "controllerName", new { Area = "areaName" });

Also don't forget to add the namespace of the controller to avoid a conflict between the admin area controller names and the site controller names.

Something like this

 public override void RegisterArea(AreaRegistrationContext context)         {             context.MapRoute(                 "Admin_default",                 "Admin/{controller}/{action}/{id}",                 new { action = "Index", id = UrlParameter.Optional },                   new[] { "Site.Mvc.Areas.Admin.Controllers" }             );         } 
like image 148
sakir Avatar answered Sep 20 '22 21:09

sakir