Frage

Ich bin auf dem Weg, eine ASP.NET -MVC -Anwendung mit der neuesten Beta -Version zu erstellen, und ich frage mich, ob es möglich ist, das Standardprojektlayout von zu ändern

/Views/home/index.aspx /views/home/about.aspx

zu

/Blog/views/home/index.aspx /blog/views/home/about.aspx

/Forum/views/home/index.aspx /forum/views/home/about.aspx

Ziel ist es, eine Trennung zwischen "Anwendungen" innerhalb eines einzelnen Webprojekts zu erreichen, so etwas wie Thomas Owens hier bereits gefragt wurde: Welche Verzeichnisstruktur würde unter einem MVC -Framework von anderen Entwicklern zu erwarten?

Natürlich sollte dies auch die Controller, nicht nur die Ansichten, umfassen.

War es hilfreich?

Lösung

This is not a new concept. It is called "areas" in Monorail. There has been a lot of buzz about this topic lately on the ATL.NET forum and elsewhere. Steve Sanderson has come up with a way to do this but apparently it leaves some issues. In reseponse, apparently the MVC team is going to take a "deep look" at it for a future release.

Andere Tipps

Yes, it should be possible to do this. I can think of one way; there may be others.

The first step is to modify the default route to include your application name:

routes.MapRoute("Default",
                "{applicationName}/{controller}/{action}/{id})",
                null, null);

I'm presuming that you're going to group the two "applications" into different namespaces within a single assembly. So you might have two namespaces like:

  • MyApp.Blog.Controllers
  • MyApp.Forum.Controllers

Next, you need to change the controller factory so that it instantiates the right controller. You can do this by subtyping the DefaultControllerFactory and overriding the GetControllerType method:

    protected override System.Type GetControllerType(string controllerName)
    {
        string applicationName;
        if (RequestContext != null && 
           RequestContext.RouteData.Values.TryGetValue(
           "applicationName", out applicationName)) {
           // return controller type using app name to 
           // look up namespace and controllerName argument
           return ...
        }

        // if no match, maybe it's a different controller/route
        return base.GetControllerType(controllerName);
    }

Finally, you need to tell MVC to use your ControllerFactory. In Global.asax.cs:

    private void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
        ControllerBuilder.Current.SetControllerFactory(
            MyApp.MyControllerFactory());
    }

Locating views can be handled similarly. In this case, you subtype WebFormViewEngine.

Ich habe gerade einen Blog -Beitrag geschrieben, der einen Ansatz für die Gruppierung von Controllern beschreibt, die den "Bereichen" in Monorail ähnelt. Es wird jedoch noch keine verschachtelten Gebiete angesprochen.

http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top