Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify an area name in an action link?

People also ask

How do I set an area in 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.

Which code statement is used to redirect to a controller action in a different area?

RedirectToAction(String, String) Redirects to the specified action using the action name and controller name.

What is a URL action?

A URL action is a hyperlink that points to a web page, file, or other web-based resource outside of Tableau. You can use URL actions to create an email or link to additional information about your data. To customize links based on your data, you can automatically enter field values as parameters in URLs.


Figured it out..

Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" }, new{})

Something I ran into right after this, that I imagine others might run into: If you need to link from within an area to an action not in an area, you still need to specify the Area as empty string.

For instance, I moved some MVC code into an area, and found I needed to update urls in the master page that referenced other pages on the site.

To specify an url to something not in an area, use

Html.ActionLink("home", "Index", new { area = "", controller = "Home" })

Use:

 Html.ActionLink("Text", "ActionName", "ControllerName", new { Area = "AreaName" }, null)

Note:4th parameter is to pass route Values, if you pass an empty parameter it will consider root structure and if you pass appropriate value it use it as area.

Also do not forget to use null or new{} as the 5th parameter because passing null or new {} while creating action link will not overload method for (text,action,controller,route data) or its (text,action,controller,route data,html attribute) so use the proper method


In MVC2 giving area="root" worked for me as below

Html.ActionLink("Home", "Index", "Home", new { Area = "root" }, new{})

A neat trick you can do if you are using an area a lot in a View is define it as a variable at the top:

@{ var awesomeArea = new { area = "Awesome" }; }

@Html.Action("Something", "Somewhere", awesomeArea)
@Html.ActionLink("Stuff", "FooBar", awesomeArea)

Here is what I came up with as a solution to allow a user to link to the pre-built authentication systems.

Each of my areas has a version of the _LoginPartial.cshtml file.

I probably could get the application to use a single version of the file, however I kept running into errors when trying to use a single login partial.

It is only a slight modification to the original generated loginpartial, but it seems to work well when used in specific areas.

Here is the code that gets used in all of them:

@if (Request.IsAuthenticated)
{
    <text>
    Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", new { area = "" }, htmlAttributes: new { @class = "username", title = "Manage" })!
    @using (Html.BeginForm("LogOff", "Account", new { area = "" }, FormMethod.Post, new { id = "logoutForm" }))
 {
        @Html.AntiForgeryToken()
        <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
 }
    </text>
}
else
{
    <ul>
        <li>@Html.ActionLink("Register", "Register", "Account", new { area = "" }, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", new { area = "" }, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}