Anupam Singh
Anupam Singh

Reputation: 1166

How to implement my own folder structure(architecture) in ASP.NET MVC Project?

As we know, when we create an MVC application, it creates its own typical structure which is known as convention over configuration and its a good practice . It configure views, controller and model separately .

My concern is, can i architect(design) it like :

enter image description here

If I do that, My viewengine will search views inside view not inside subfolders and there are so many things like routing will get changed.. and so on..

Actually I dont want to construct my view,controller or model in a typical way, I want to put my view separately according to my domain, not according to controller like MVC does. However in case of controller we can use any folder structure . I am specific about model,views and routing should not be affected as well.

And it is all about "Convention over My own Configuration".

Can someone please explain, how to get it done or any other alternatives.

Thanx Anupam.

Upvotes: 1

Views: 1455

Answers (3)

Anupam Singh
Anupam Singh

Reputation: 1166

So far I have reached to conclusion.

The thing we have to consider for this are :

1.Need to override ControllerFactory : we have to search controller in specific location or assembly reference.(by default controller factory just chak the controller name )

2.Need to override ViewEngine : We have to change view search location according to our need.

3.Little modification in Route : we have to specify module name in routs for proper redirection. route will be something like :

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

will implement it soon. Your suggestions are most welcome.

Upvotes: 0

JuhaKangas
JuhaKangas

Reputation: 883

The location and folder structure of your controllers and models doesn't really matter, should work either way. Controllers are located by their type and classname.

The viewengine by default does search subfolders, trying to match with the naming of your controller. It searches multiple locations.

Now, if you want to change how the view engine searches for files you can configure it in global.asax. Have a look here regarding RazorViewEngine for example.

Personally I have gone away from the view engine auto locating my views and instead use relative paths for all of them because I think it makes it more readable overall.

Below is an example of a configured view engine and a relative path.

global.asax

ViewEngines.Engines.Clear();
var razorEngine = new RazorViewEngine() { FileExtensions = new string[] { "cshtml" } };
ViewEngines.Engines.Add(razorEngine);

controller action

return View("~/Views/Home/Index.cshtml", model);

Hope I understood your question correctly.

Upvotes: 2

Paddy
Paddy

Reputation: 33857

It sounds like what you are looking for is 'Areas'. This allows you to separate your controllers & views into separate 'area' folders.

More information can be found here, as including the necessary information to get this set up in this answer is probably not practical:

http://msdn.microsoft.com/en-GB/library/ee671793(v=vs.100).aspx

Upvotes: 4

Related Questions