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.