0

I created two areas (with controllers, views, registration, etc) with VS's scaffolding process. One for homecontroller and one called account.

enter image description here

I added a single line "route.DataTokens" to the routeconfig.cs file to connect to the homecontroller on startup.

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

Here is my HomeAreaRegistration.cs RegistrationArea method

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

But in the url of the browser I see

https://localhost:44301/Home/Home/Index

What I want to see is

https://localhost:44301/Home/Index

What do I modify to get this?

Edit - Solved! I think. Here is what I did. In HomeArearegistration.cs I removed the Home/

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

Is this the proper way to go about doing this because something doesn't seem right here, if I type

https://localhost:44301/Home/Index.cshtml

which I would expect to take me to the home page, I get a HTTP 404 error not found but if I type

https://localhost:44301/Home/Index

It take me to the home page

chuckd
  • 13,460
  • 29
  • 152
  • 331
  • 1
    `../Home/Index.cshtml` does not work because MVC routing works by navigating to action methods, not files. –  Oct 22 '16 at 01:09
  • ok, maybe I just don't understand the routing completely. But let me ask this question. Sometimes when the page isn't found I see a message that says something like "Page Not Found after tyring ~/Home/Index.html, ~Home/Index.cshtml, etc, etc", so shouldn't I be able to type in one of these paths and assuming it's routed correctly connect to the pag?. Ex. if I type localpath/Home/Index.cshtml I should be taken to the page? – chuckd Oct 22 '16 at 01:40
  • 1
    No. MVC works by navigating to action methods, not files. So navigating to `../Home/Index` takes you to the `Index()` method of `HomeController` and that methods returns the view (by using `return View();`) –  Oct 22 '16 at 01:43
  • ok thank you for the explanation – chuckd Oct 22 '16 at 01:47

0 Answers0