0 asp.net asp.net-mvc asp.net-mvc-2
我正在使用MVC 2,我需要将默认名称"Areas"更改为<MyOwnAreaName>.
是不是可以将默认名称"区域"更改为我自己的名字?
任何人都可以帮助提供解决方案,提前谢谢.
您可以编写自定义虚拟路径提供程序.以下是您可能有兴趣覆盖的默认值:
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",
};
Run Code Online (Sandbox Code Playgroud)
所以这就是你的情况看起来如何:
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",
};
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Application_Start以下位置注册此自定义引擎:
protected void Application_Start()
{
...
ViewEngines.Engines.Add(new CustomViewEngine());
}
Run Code Online (Sandbox Code Playgroud)
现在您可以放置区域文件了~/MyOwnAreaName.
备注/建议:尽可能坚持ASP.NET MVC约定并仅在严格必要时覆盖它们.