2
votes

I've created a new ASP.NET MVC 5 using Visual Studio 2015. All the code in the project is default which Microsoft provides as part of their template but when I add a new controller called UserController with [RouteArea("Admin")] the other links will stop working such as Home/Contact, Home/About once I visit the Admin area (/Admin/User/Index) but if I manually type in the address bar http://localhost/ then the About and Contact links work fine. It's only when you enter a controller that has been a RouteArea attribute. I'm new to MVC but I'm much more comfortable with WebForms as I've built websites using that for years.

I even added [Route] above the actions along with [HttpGet] to see if that solves the problem but that didn't work.

The links in shared/_layout.cshtml is still the default which resolve URLs before navigating to a controller that has a [RouteArea]:

@Html.ActionLink("Home", "Index", "Home") // => <a href="/">Home</a>
@Html.ActionLink("About", "About", "Home") // => <a href="/About">About</a>
@Html.ActionLink("Contact", "Contact", "Home") // => <a href="/Contact">Contact</a>

HomeController.cs:

public class HomeController : Controller
{
    //GET /
    [HttpGet]
    [Route]
    public ActionResult Index()
    {
        return View();
    }

    //GET /About
    [HttpGet]
    [Route("About")]
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    //GET /Contact
    [HttpGet]
    [Route("Contact")]
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

Once I enter the controller below, the Html.ActionLink() mentioned above can't resolve the URL to the controller above.

UserController.cs:

[RouteArea("Admin")]
[RoutePrefix("User")]
public class UserController : Controller
{
    //GET Admin/User/Index
    [HttpGet]
    [Route("Index")]
    public ActionResult Index()
    {
        return View();
    }
}

The Html.ActionLinks for the top navigation menu, after entering the UserController, all have the href attribute as empty as shown below:

@Html.ActionLink("Home", "Index", "Home") // => <a href="">Home</a>
@Html.ActionLink("About", "About", "Home") // => <a href="">About</a>
@Html.ActionLink("Contact", "Contact", "Home") // => <a href="">Contact</a>

As you can see from the html output in the above 3 lines on the right side that there is no URL.

Any help would be appreciated. Thanks.

2
Do you need to specify the 'Area'? eg. @Html.ActionLink("User", "Index", "User", new {area = "Admin"}) - AntDC
The Html.ActionLink resolves the user admin area without any problem but once I'm there (Admin/User/Index); home, contact and about stop working. I'll try and see if that helps. Thanks - Silent Fart

2 Answers

1
votes

When using areas, I would recommend using the overload of ActionLink that allows you to specify the area.

So, your navigation links pointing to the root will be :

@Html.ActionLink("Home", "Index", "Home", new {area=""}, new {}) 
@Html.ActionLink("About", "About", "Home", new {area=""}, new {}) 
@Html.ActionLink("Contact", "Contact", "Home", new {area=""}, new {})

Or

@Html.ActionLink("Home, "Index", new { controller="Home", area=""})
@Html.ActionLink("About, "About", new { controller="Home", area=""})
@Html.ActionLink("Contact, "Contact", new { controller="Home", area=""})

This way they will still work when you navigate into an area.

0
votes

The answer was (thanks to @AntDC and @Slicksim for pointing me in the right direction):

<li>@Html.ActionLink("Home", "Index", "Home", new {area = ""}, null)</li>
<li>@Html.ActionLink("About", "About", "Home", new { area = "" }, null)</li>
<li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, null)</li>

For whatever reason, ASP.NET was taking the new {area = ""} as a html attribute even though there's a signature with this precise overload for routeValues.