Folder structure with large mvc application

Last post 09-13-2008 2:32 PM by levib. 9 replies.

Sort Posts:

  • Folder structure with large mvc application

    09-01-2008, 3:48 AM

    I am seeking to migrate a large asp.net application to the MVC framework. The application consists of 20 (or so) modules.

    The structure have in mind is expressed in the partial folder structure below:

    Project
          Content
    Core
          Content
          Models
          Controllers
                NewsSender
                      MailingsController.cs
                      ContactsController.cs
                CMS
                WebShop
          Views
                NewsSender
                      Mailings
                            List.aspx
                            Create.aspx
                      Contacts
    etc.

    I have three questions:

    • Is it possible to have the controllers and views in a subfolder instead of the root?
    • Is it possible to have controllers and views per module?
    • Is there a better way of implementing this?

    Any guidance is appreciated!

    Many thanks in advance,

    Florian

  • Re: Folder structure with large mvc application

    09-01-2008, 11:57 AM
    Answer
    • Contributor
      4,358 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 881
    • ASPInsiders
      TrustedFriends-MVPs

    You can put your Controllers anywhere in the project. As long as it gets compiled into the dll. The only problem with splitting them up is that if you have 2 controllers with the same name in different namespaces the ControllerFactory wont know which one to create unless you specify the namespace in the route

      routes.MapRoute(
        "NewsSender.Mailings",
        "{controller}/{action}/{id}",
        new {controller = "Mailings", action = "Index", id = ""} /* defaults */,
        new {controller = "Mailings|Contacts|..." } /* constraints */,
        new [] { "App.Controllers.NewsSender" } /* namespaces */
      )

    The Views can be anywhere as well... as long as the ViewEngine knows where to look. For example you can override the OnResultExecuting method on the Controller and set the view location formats on the View Engine as follows

    public class MailingsController:Controller
    {
      protected override void OnResultExecuting(ResultExecutingContext filterContext)
      {
        var result = filterContext.Result as PartialViewResult;
        if( result == null ) return;
        var viewEngine = result.ViewEngine = new WebFormViewEngine();
    
        var prefix = "~/Views/" + moduleName + "/{1}/{0}";
        var prefixShared = "~/Views/" + moduleName + "/Shared/{0}";
    
        viewEngine.MasterLocationFormats = new [] { prefix + ".master", prefixShared + ".master" };
        viewEngine.ViewLocationFormats = new [] { prefix + ".aspx", prefix + ".ascx", prefixShared + ".aspx", prefixShared + ".ascx" };
        viewEngine.PartialViewLocationFormats = viewEngine.ViewLocationFormats;
      }
    ...

    In the location formats above, {0} is replaced with the view name, and {1} is replaced with the controller name. You might want to put this logic into a common base controller class so you don't have to repeat it everywhere.

     

     

  • Re: Folder structure with large mvc application

    09-02-2008, 2:26 AM
    • Member
      4 point Member
    • sgentile
    • Member since 08-10-2008, 1:01 PM
    • Posts 6

    tgmdbm:
    You can put your Controllers anywhere in the project. As long as it gets compiled into the dll

    Actually is totally possible to put the controllers in a separate dll.

    In this way it's more clear the MVC separation (I also put the Model is in another dll).


  • Re: Folder structure with large mvc application

    09-07-2008, 12:27 PM
    • Member
      20 point Member
    • vyrotek
    • Member since 10-01-2006, 7:53 PM
    • Posts 6

    update: Nevermind! :)

  • Re: Folder structure with large mvc application

    09-08-2008, 2:23 AM
    • Member
      6 point Member
    • Andrew Tobin
    • Member since 09-08-2008, 6:21 AM
    • Posts 3

    Hey mate, I'm doing what you've advised here, and I was just wondering how I can refer to the correct namespace using the Html.ActionLink if I have duplicate controller names in seperate namespaces?

    Am I heading down the wrong path and need to use something else?

  • Re: Folder structure with large mvc application

    09-08-2008, 2:33 AM
    Answer
    • Contributor
      4,358 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 881
    • ASPInsiders
      TrustedFriends-MVPs

     Use Html.RouteLink( "NewsSender.Mailings", ... )

  • Re: Folder structure with large mvc application

    09-08-2008, 3:50 AM
    • Member
      6 point Member
    • Andrew Tobin
    • Member since 09-08-2008, 6:21 AM
    • Posts 3

    Okay, so I see that, but how do I define a specific action on the route?

    Using RouteLink I get route name, link text and objects for HtmlAttributes, but I don't get to define which action on the route I want?

    Sorry, I'm a definite newbie to ASP, so it's probably a stupid question :)

  • Re: Folder structure with large mvc application

    09-08-2008, 4:12 AM
    • Member
      6 point Member
    • Andrew Tobin
    • Member since 09-08-2008, 6:21 AM
    • Posts 3

    Okay, it was a dumb moment, I just do the RouteValueDictionary to point out the controller, action, id, etc, same as when defining the defaults on the routes, etc.

    It came to me when I wasn't thinking about it, as usual. 

    Thanks for the help!

  • MVC on VS 2005????????????

    09-13-2008, 8:12 AM

    Hi,

    Regarding MVC in asp.net on VS2005 ?   After long hrs searching on net i came accros that it is not possible to create an MVC app in VS2005, so there any other way to implement MVC in VS2005. Plz giude me thru this issue.

     

    Jackson C. 

    Jack...!!!
  • Re: MVC on VS 2005????????????

    09-13-2008, 2:32 PM
    • Contributor
      4,312 point Contributor
    • levib
    • Member since 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 764
    • AspNetTeam

    jacksoncoutinho:
    Regarding MVC in asp.net on VS2005 ?   After long hrs searching on net i came accros that it is not possible to create an MVC app in VS2005, so there any other way to implement MVC in VS2005. Plz giude me thru this issue.

    Jackson - Since this is unrelated to the topic at hand for this thread, please open a new thread if you wish to discuss this.  Thanks!

Page 1 of 1 (10 items)