How to pass Area in Url.Action?

asp.net MvcHtml Helperhtml.actionlinkurl.action

asp.net Mvc Problem Overview


asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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" }
            );
        }

Solution 2 - asp.net Mvc

@Url.Action("{action}", "{controller}", new { Area = "areaname" });
@Html.ActionLink("LinkName", "{action}", "{controller}", new { area = "{areaname}" }, new { @class = "btn btn-cool" })

write area name as html attribute with anonymus object. you can use actionlink html helper extension method to achieve same thing.

Solution 3 - asp.net Mvc

If you want to create link for root controllers, it's enough to use this code:

Url.Action("ShowImage", "Page", new { Area = "" })

Solution 4 - asp.net Mvc

@Url.Action("{action}", "{controller}", new { Area = "areaname" });

@Html.ActionLink("LinkName", "{action}", "{controller}", new { area = "{areaname}"}, new { @class = "btn btn-cool" })

You can use above this

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondoncadavonaView Question on Stackoverflow
Solution 1 - asp.net MvcsakirView Answer on Stackoverflow
Solution 2 - asp.net MvcHardikView Answer on Stackoverflow
Solution 3 - asp.net MvcMajid BasiratiView Answer on Stackoverflow
Solution 4 - asp.net MvcSam pandyaView Answer on Stackoverflow