开发者

How to reduce number of sub-folders in MVC Areas?

I have an asp.net MVC3 application, and I'm using Areas.

By default, I understand the setup would look like this:

Areas\
   Orders\
       Controllers\
           HomeController.cs     (Action method: Index)
           OrderController.cs   (Action method: OrderIndex)
           TransferController.cs (Action method: TransferIndex)
       Views\
           Home\
               Index.aspx
           Order\
               OrderIndex.aspx
           Transfer\
               TransferIndex.aspx

What I would like to do is simply the number of folders to do something like this:

Areas\
   Orders\
       Controllers\
           OrderContro开发者_运维问答ller.cs   (Action method: Index, OrderIndex, TransferIndex)
       Views\
           Index.aspx
           OrderIndex.aspx
           TransferIndex.aspx

Is this possible? Is this a routing setup?


You could write a custom view engine. I will provide an example with Razor:

public class MyViewEngine : RazorViewEngine
{
    public MyViewEngine()
    {
        base.AreaViewLocationFormats = base.AreaViewLocationFormats.Union(new[] 
        {
            "~/Areas/{2}/Views/{1}{0}.cshtml"    
        }).ToArray();
    }
}

and then register this view engine in Application_Start:

ViewEngines.Engines.Add(new MyViewEngine());

and that's pretty much all you're gonna need to achieve what you are looking for.

And of course if you are using the WebForms view engine slight adaptations are necessary:

public class MyViewEngine : WebFormViewEngine
{
    public MyViewEngine()
    {
        base.AreaViewLocationFormats = base.AreaViewLocationFormats.Union(new[] 
        {
            "~/Areas/{2}/Views/{1}{0}.aspx"
        }).ToArray();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜