如何减少MVC区域中的子文件夹数量?

发布于 2024-11-10 10:32:21 字数 765 浏览 0 评论 0原文

我有一个 asp.net MVC3 应用程序,并且我正在使用 Areas。

默认情况下,我理解设置如下所示:

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

我想做的只是文件夹的数量来执行类似的操作:

Areas\
   Orders\
       Controllers\
           OrderController.cs   (Action method: Index, OrderIndex, TransferIndex)
       Views\
           Index.aspx
           OrderIndex.aspx
           TransferIndex.aspx

这可能吗?这是路由设置吗?

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\
           OrderController.cs   (Action method: Index, OrderIndex, TransferIndex)
       Views\
           Index.aspx
           OrderIndex.aspx
           TransferIndex.aspx

Is this possible? Is this a routing setup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金兰素衣 2024-11-17 10:32:21

您可以编写自定义视图引擎。我将提供一个使用 Razor 的示例:

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

然后在 Application_Start 中注册此视图引擎:

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

这几乎就是您实现所需目标所需的全部内容。

当然,如果您使用 WebForms 视图引擎,则需要进行一些细微的调整:

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

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文