如何更改默认名称“区域”到“区域”在MVC2中?

发布于 2024-10-18 10:07:40 字数 144 浏览 0 评论 0原文

我使用的是 MVC 2,我需要将默认名称“Areas”更改为

无法将默认名称“Areas”更改为我自己的名字吗?

哪位大佬能帮忙解答一下吗,先谢谢了。

I'm using MVC 2, I need to change the default name "Areas" to <MyOwnAreaName>.

Is't possible to change default name "Areas" to my own name??

Can any one help to give the solution, thanks in advance.

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

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

发布评论

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

评论(1

忆梦 2024-10-25 10:07:40

您可以编写一个自定义虚拟路径提供程序 。以下是您可能有兴趣覆盖的默认值:

MasterLocationFormats = new[] 
{
    "~/Views/{1}/{0}.master",
    "~/Views/Shared/{0}.master"
};

AreaMasterLocationFormats = new[] 
{
    "~/Areas/{2}/Views/{1}/{0}.master",
    "~/Areas/{2}/Views/Shared/{0}.master",
};

ViewLocationFormats = new[] 
{
    "~/Views/{1}/{0}.aspx",
    "~/Views/{1}/{0}.ascx",
    "~/Views/Shared/{0}.aspx",
    "~/Views/Shared/{0}.ascx"
};

AreaViewLocationFormats = new[] 
{
    "~/Areas/{2}/Views/{1}/{0}.aspx",
    "~/Areas/{2}/Views/{1}/{0}.ascx",
    "~/Areas/{2}/Views/Shared/{0}.aspx",
    "~/Areas/{2}/Views/Shared/{0}.ascx",
};

因此,在您的情况下,这可能是这样的:

public class CustomViewEngine : WebFormViewEngine
{
    public CustomViewEngine()
    {
        AreaMasterLocationFormats = new[] 
        {
            "~/MyOwnAreaName/{2}/Views/{1}/{0}.master",
            "~/MyOwnAreaName/{2}/Views/Shared/{0}.master",
        };

        AreaViewLocationFormats = new[] 
        {
            "~/MyOwnAreaName/{2}/Views/{1}/{0}.aspx",
            "~/MyOwnAreaName/{2}/Views/{1}/{0}.ascx",
            "~/MyOwnAreaName/{2}/Views/Shared/{0}.aspx",
            "~/MyOwnAreaName/{2}/Views/Shared/{0}.ascx",
        };
    }
}

然后在 Application_Start 中注册此自定义引擎:

protected void Application_Start()
{
    ...
    ViewEngines.Engines.Add(new CustomViewEngine());
}

现在您可以将区域文件放在 中~/MyOwnAreaName

备注/建议:尽可能遵守 ASP.NET MVC 约定,仅在绝对必要时才覆盖它们。

You could write a custom virtual path provider. Here are the default values that you might be interested in overriding:

MasterLocationFormats = new[] 
{
    "~/Views/{1}/{0}.master",
    "~/Views/Shared/{0}.master"
};

AreaMasterLocationFormats = new[] 
{
    "~/Areas/{2}/Views/{1}/{0}.master",
    "~/Areas/{2}/Views/Shared/{0}.master",
};

ViewLocationFormats = new[] 
{
    "~/Views/{1}/{0}.aspx",
    "~/Views/{1}/{0}.ascx",
    "~/Views/Shared/{0}.aspx",
    "~/Views/Shared/{0}.ascx"
};

AreaViewLocationFormats = new[] 
{
    "~/Areas/{2}/Views/{1}/{0}.aspx",
    "~/Areas/{2}/Views/{1}/{0}.ascx",
    "~/Areas/{2}/Views/Shared/{0}.aspx",
    "~/Areas/{2}/Views/Shared/{0}.ascx",
};

So here's how this might look in your case:

public class CustomViewEngine : WebFormViewEngine
{
    public CustomViewEngine()
    {
        AreaMasterLocationFormats = new[] 
        {
            "~/MyOwnAreaName/{2}/Views/{1}/{0}.master",
            "~/MyOwnAreaName/{2}/Views/Shared/{0}.master",
        };

        AreaViewLocationFormats = new[] 
        {
            "~/MyOwnAreaName/{2}/Views/{1}/{0}.aspx",
            "~/MyOwnAreaName/{2}/Views/{1}/{0}.ascx",
            "~/MyOwnAreaName/{2}/Views/Shared/{0}.aspx",
            "~/MyOwnAreaName/{2}/Views/Shared/{0}.ascx",
        };
    }
}

and then register this custom engine in Application_Start:

protected void Application_Start()
{
    ...
    ViewEngines.Engines.Add(new CustomViewEngine());
}

Now you could place the area files in ~/MyOwnAreaName.

Remark/Advice: stick to the ASP.NET MVC conventions as much as possible and override them only if strictly necessary.

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