1

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.

2
  • See mvccoderouting.codeplex.com Commented Sep 2, 2014 at 14:08
  • Is it the same to say folder structure than architecture? How? Commented Jul 31, 2021 at 15:33

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanx for your reply Paddy, But i am aware about areas . Areas again create a typical architecture (like mini MVC architecture). But I dony want it. What I want is, My own structure with existing convention . Means my configuration over its default convention.
2

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.

4 Comments

Thanx JuhaKangas, Yes you did understood my concern correctly. I was aware that controller are compiled so it doesn't matter where we put those but important part was view and its rendering. However i have extended RazorViewEngine and set ViewLocationFormats = new string[]{ /* my custom searchlocations*/} but I was looking for the better way. Did you ever use any other way to tweak it .Please share if you did.
Not really. But what is the problem you are having when tweaking the view engine? Could you perhaps specify in your post exactly how you want the views to be located from your controller.
Nothing but it seemed was loosing decoupling somewhere.According to you if I use "new RazorViewEngine() { FileExtensions = new string[] { "cshtml" } };" will search all the files but what if I have same file names inside Sales and Purchase. I think I should work on it to make it perfect. I believe for a good architecture and a large project we should consider the structure I have mentioned.
No I didn't say it will search everywhere, I was just showing an example of viewengine changes. You will have to read up on how to make it do what you want, or ask a more specific question about what you want it to do.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.