我使用的是MVC2,我需要将默认名称"Areas“更改为<MyOwnAreaName>。
不能将默认名称"Areas“更改为我自己的名称吗??
有没有人能帮忙给出解决方案,提前谢谢。
发布于 2011-02-21 01:03:50
你可以写一个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约定,只有在严格必要的情况下才覆盖它们。
https://stackoverflow.com/questions/5058461
复制相似问题