如何在MVC2中将默认名称“区域”更改为“<myownareaname> Areas”?</myownareaname>

时间:2011-02-20 16:54:23

标签: asp.net asp.net-mvc asp.net-mvc-2

我正在使用MVC 2,我需要将默认名称“区域”更改为<MyOwnAreaName>

是否可以将默认名称“区域”更改为我自己的名字?

任何人都可以帮助提供解决方案,提前谢谢。

1 个答案:

答案 0 :(得分:5)

你可以写一个custom virtual path provider。以下是您可能有兴趣覆盖的默认值:

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约定并仅在必要时才覆盖它们。