0

So I am reading about building MVC3 projects and there is one thing that seriously bugs me. The folder structure of the project in no way corresponds to the path of the HTTP request. There is a bunch of things I like and would want to use, but having a flat folder structure is not one of them.

Why is this a problem? Well, I see it becoming a problem when building a site with a heavy mix of content pages and various forms/dynamic pages (most of our sites are like that), which would typically be done by different people. It seem it would be too complicated for client-side developers to follow routing rules of dynamic pages and/or creating new ones.

What I would like to understand is if there is way to configure MVC3 application in such a way that:

  1. it follows directory structure for finding controllers without explicit route map for each one
  2. views live in the same folder as corresponding controller
  3. routing magic still works for actions and parameters

For instance I'd like to have a request /fus/ro/dah/ to try to find DahController in the \webroot\fus\ro\dah\ folder and execute its Index action. If not found it would look for RoController with Dah action in the \webroot\fus\ro\ folder, etc.

It is entirely possible that MVC was not meant to be working this way at all and I am just trying to force a square peg into a round hole.

UPDATE: Looks like I can drop a view file into the desired folder structure, and it will be executed. However layout would not work apparently because it is expecting a controller. Does this mean I have to create a controller for pure content pages? That is a pretty crappy design...

UPDATE 2: Main issue right now is that creating "fus" folder means that MVC will not even attempt to look for FusController... not under "fus" folder, nor anywhere else. Is it possible to get around that?

4
  • You have to change the way you think. Creating controller, action and one view file is not a problem. Commented Jun 3, 2012 at 2:19
  • @LukLed For me it isn't. For people who only understand HTML/CSS/JavaScript that would be a major problem. They have issues with much simpler concepts already. Regardless of that, copy-pasting boilerplate code is never a good design choice. Commented Jun 3, 2012 at 2:24
  • 1
    I am sorry, but if someone doesn't understand that, probably doesn't understand HTML/CSS/Javascript well to. They have to understand new concepts, otherwise they will be useless in few years. Commented Jun 3, 2012 at 2:31
  • @LukLed Thanks, I have enough power to choose framework for my projects, not fire people. Commented Jun 3, 2012 at 3:04

5 Answers 5

1

For instance I'd like to have a request /fus/ro/dah/ to try to find DahController in the \webroot\fus\ro\dah\ folder and execute its Index action. If not found it would look for RoController with Dah action in the \webroot\fus\ro\ folder, etc.

MVC is not designed for a particular need like this, it is a general framework for building applications using model-view-controller pattern.

If you can't bend the application for the framework you can bend the framework for the application and honestly MVC is very customizable. [As a proof, in the current project (migration from ASP to MVC) that I'm working we have models as xml and no classes also we are using XSLTs for rendering. With a little work we have created custom components like custom view engine, custom validation provider, custom model binder... to make the framework best fit for the application and it does]

MVC is not designed and not forces to use it as it is and you can customize/extend as much you want. In your case you may have to create a

custom controller factory (because you want to customize the way in which the controller is seleced),

custom view engine (because you want to customize where the view is placed)

and may be others.

For custom controller factory you have to extend the DefaultControllerFactory class. There are lot of articles you can find through Google that explains about how to create custom controller factories.

Depending upon the view engine you are using you have to extend the respective one. For ex. if you are using web forms then you have to extend the WebFormsViewEngine and it razor then RazorViewEngine.

For more info. check this link

http://codeclimber.net.nz/archive/2009/04/08/13-asp.net-mvc-extensibility-points-you-have-to-know.aspx

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

1 Comment

Do you have any specifics? What classes should I be looking for?
1

you can mixup Asp.net and Asp.net MVC. as LukLed said, MVC is convention over configuration pattern. if you follow the convention. you dont need to configure. you can check this link for mixing up the asp.net content with MVC3

Mixing Asp.net and Razor

3 Comments

this seems like an acceptable workaround for dealing with legacy sites, but I wouldn't be starting a new project with this. Layout vs Master pages alone is enough of a hassle.
Why do you need to use MVC at all? Can't you just stay with ASP.NET Web Forms?
@BrianBehm Razor view engine, model/parameter binding, controller actions easy to configure for json/xml/partial view output.
0

I believe ASP.NET MVC is not meant to be used that way. While you can configure MVC to do it, it is better to keep standard /controller/action/parameters URL format. If you have complex website with many different functionalities, areas may be helpful http://msdn.microsoft.com/en-us/library/ee671793.aspx. Every area get its own set of controllers, models and views, so teams working on different parts of website won't disturb each other.

While it may sound convenient, that framework first searches for DahController and executes Index action, then searches for another one, I find it bad idea. URLs should be clearly defined and Fus controller with Ro action shouldn't just stop working, because someone created RoController with Index action.

2 Comments

How would you configure MVC to do it?
@IliaG: That would probably require writing your own route handler, replacing existing one, which is generally not good idea. You really shouldn't go that way.
0

Look into using Areas as well. I think that helped me get over my folder structure issues with MVC honestly. So i could use the base folder as my Home details, then i could have a 'Admin' area which was a separate folder, things like that.

Comments

0

How "regular ASP.net" do you want it to be? If you want to do "legacy" ASP.Net Web Forms mixed in with MVC, you certainly can - re: mixing MVC with "file based aspx" - aka "hybrid". At the end of the day, it's all ASP.Net.

This is a standard MVC Application generated by Visual Studio. I've added a folder somedirectory where I want to use the legacy folder/file.ext paradigm and have a default.aspx Web Forms file:

MVC scaffolding by VS with physical directory

  • Can I navigate to it via http://foo.com/somedirectory? Yes.
  • Can I use "pretty urls" too? Yes

Vanilla Global.asax generated by VS, just added MapPageRoute:

....

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //using "pretty urls" - ordering your routes matter.
    routes.MapPageRoute("theWebForm", "legacy", "~/somedirectory/default.aspx");

    routes.MapRoute(
        "Default", 
        "{controller}/{action}/{id}", 
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
}
  • so now I can navigate to it via http://foo.com/legacy

Just check the order of your routes, and perhaps plan on your naming conventions so you don't have "collisions"...

Hth....

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.