Simone
Simone

Reputation: 2430

Mvc Routing does not work even if the route is matched

I am trying to create a new route... Here the code:

routes.MapRoute(
    name: "Services",
    url: "Administration/{controller}/{action}/{id}",
    defaults: new { controller = "Services", action = "Index", id = UrlParameter.Optional });

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

Now when I click on an actionlik, it redirects me on the correct controller:

@Html.ActionLink("Services", "Index","Services")

Here the Index Action in the Services controller

public ActionResult Index()
{
    return View();     //Here it is where I stop debug
}

I arrive in the action.. and now my custom route should redirect to my view. Correct? I let you see what I see when i stop debug:

enter image description here

How you can see everything seems is well valorized. But when I obtain following error:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Services/Index.aspx
~/Views/Services/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Services/Index.cshtml
~/Views/Services/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

It does not look the view inside Administration folder!! In this way it works instead:

return View("~/Views/Administration/Services/Index.cshtml");

Where is wrong?

Thank you

Upvotes: 0

Views: 67

Answers (1)

DavidG
DavidG

Reputation: 118957

Looks like you don't quite understand routing, or that routing is not the same thing as view resolution. Changing the URL to access an action method doesn't make it search new folders for your views. So you need to do one of the following:

  1. Leave the view in one of the folders the error message suggests.
  2. Manually specify the view: return View("~/Views/Administration/Services/Index.cshtml");
  3. Learn how to use MVC areas.
  4. Manually add in your additional folders to the list the view engine uses.
  5. Implement your own view engine.

Personally I recommend option 1.

Upvotes: 1

Related Questions