Shawn de Wet
Shawn de Wet

Reputation: 5976

Finding my View in asp.net mvc

My Views folder has gotten crazy big! I would like to reorganize it so that the Views folder contains a list of Modules, and then each Module folder contains its share of the View (Controller) folders that currently appear under Views folder.

But of course this means going into each of my controllers and editing every view-returning method the explicit location of its view.

So instead of Controller Orders.Index method just having this:

return View();

I have to edit it to return this:

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

You can imagine the suck level that this exercise attains over 50 or so controllers.

Is there some way that I can setup a routing or something per controller that will tell that controller's methods to go find their views in a defined subfolder of the Views folder?

Upvotes: 2

Views: 82

Answers (2)

K D
K D

Reputation: 5989

It can be done with the help of CustomViewEngine Follow this post and i hope you can provide your own locations to locate the view template. MVC provide way where we can easily provide list of path to be searched

Locate view

Once you add CustomViewEngine, register it in Application_Start() event and then you are done :) Happy coding

Upvotes: 2

CodeCaster
CodeCaster

Reputation: 151588

You could fix that by implementing a custom RazorViewEngine, where you can specify the search path for the views per request, per controller and so on.

Upvotes: 1

Related Questions