Question

I added an area to my application. It is called "Examples". There is a default route:

context.MapRoute(
    "Examples_default",
    "Examples/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);

This works fine if I have a controller called "MyExampleController.cs" and a View which is by default placed under Views/MyExample/Index.cshtml. I can reach the url "http://localhost/Examples/MyExample/Index".

What I want is just create a subfolder under Views to add one more level of organisation. I.e. under Views create a folder "NewExamples" and move MyExample there. So I may have the following structure

Views/NewExamples/MyExample/Index.cshtml
Views/OldExamples/Example123/Index.cshtml

etc.

I want to reach a url "http://localhost/Examples/NewExamples/MyExample/Index".

I'm struggling with a route. My approach was to add a route before the default one:

context.MapRoute(
    "Examples_newexamples",
    "Examples/NewExamples/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);

But that does not work. I hit my controller, but then I get the server error

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Areas/Examples/Views/MyExample/Index.aspx
~/Areas/Examples/Views/MyExample/Index.ascx
~/Areas/Examples/Views/Shared/Index.aspx
~/Areas/Examples/Views/Shared/Index.ascx
...
~/Areas/Examples/Views/MyExample/Index.cshtml
~/Areas/Examples/Views/MyExample/Index.vbhtml
~/Areas/Examples/Views/Shared/Index.cshtml
~/Areas/Examples/Views/Shared/Index.vbhtml
...

So it doesn't even try to look into the NewExamples subfolder.

Is there a correct route to include this subfolder?

Was it helpful?

Solution

I was generally useful Route attribute for this purpose:

[Route("~/Examples/NewExamples/MyExample/{id}")]
public ActionResult MyExample(it id)
{
 ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top