简体   繁体   中英

Using MVC3 with folder structure like regular ASP.NET

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?

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

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

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.

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.

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:

VSC的MVC脚手架与物理目录

  • 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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM