0

I have two areas home and account.

enter image description here

When I'm on the Index.cshtml page of the home controller and I want to go to my account controller's Login.cshtml page I use this actionlink to go to the login action method which should view Login.cshtml

@Html.ActionLink("Log in", "Login", "Account", routeValues: new { area = "Account", returnUrl = Request.RawUrl }, htmlAttributes: new { id = "loginLink" })

and in the browser I see

https://localhost:44301/Account/Login?returnUrl=%2Fhome%2Findex

but I get a HTTP 404 error, not found.

Here is my AccountAreaRegistration.cs page RegistrationArea method

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Account_default",
        "{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

Here is my routeconfig.cs

var route = routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        route.DataTokens["area"] = "Home";

FYI - If I change the lines in HomeAreaRegistration and AccountAreaREgistration to

"Home/{controller}/{action}/{id}", "Account/{controller}/{action}/{id}",

I can successfully route to and connect to login.cshtml, but I'm then connecting to

localhost/Home/Home/Index localhost/Account/Account/Login

but I want to connect with only one Home and one Account

chuckd
  • 13,460
  • 29
  • 152
  • 331
  • The purpose of areas is to logically group your controllers and methods, so your structure does not really make sense. Why would you have an `Account` area with an `AccountController`? –  Oct 22 '16 at 02:24
  • I'm not sure, currently I have one folder in the project called controllers and it contains all the controllers for the whole site. I thought by creating different areas and putting the different controllers in these areas I would be creating better organization within the project structure. I don't know if this type of organization is overkill or if I'm just not doing it correctly?? – chuckd Oct 22 '16 at 02:34
  • You don't typically name the area the same as any of the controllers within it. I don't know what you app does, but for example a shopping cart site might have its main area for users to login, look at and purchase products. But then you might have an area for `Suppliers` that contains controllers/methods for managing their products (and would not be available to normal users) and another area for `Reviews` where users can view and add reviews for products. –  Oct 22 '16 at 02:56
  • so I can do some renaming, but could that be the reason why I get a error when trying to go from my /home/index page to the account/login page, because I've named the area the same as the controller inside it?? – chuckd Oct 22 '16 at 03:25
  • It does not matter if its the same name (but it is confusing). In order to navigate to the `Login()` method of `AccountController` in `Account` area, you do need `../Account/Account/Login` and `url: "Account/{controller}/{action}/{id}"` as you have discovered. Without it, there is no way that the routing engine could uniquely identify the correct route. –  Oct 22 '16 at 03:40
  • if that's the case, how does it recognize /Home/Index when I load up the page? FYI - it's /Home/Index and not /Home/Home/Index because I removed the Home/ part of the path in the HomeAreaRegistration.cs file. I thought I could just do the same thing to the AccountAreaRegistration.cs page? – chuckd Oct 22 '16 at 03:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126376/discussion-between-stephen-muecke-and-user1186050). –  Oct 22 '16 at 03:53

0 Answers0