默认情况下asp.net mvc会去View目录下Controller对应文件夹下查找文件 action名字对应的.cshtml文件
如果需要修改路径,如View/UserCenter/ControllerName/Action.cshtml
需要添加自定义视图引擎
protected void Application_Start()
{
ViewEngines.Engines.Add(new UCEngine());
}
public class UCEngine : BuildManagerViewEngine
{
private static string[] NewAreaViewFormats = new string[] { "/Views/UserCenter/{1}/{0}.cshtml" };
public UCEngine()
: this(null)
{
}
public UCEngine(IViewPageActivator viewPageActivator)
: base(viewPageActivator)
{
ViewLocationFormats = new[] {
"~/Views/UserCenter/{1}/{0}.cshtml",
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
PartialViewLocationFormats = new[] {
"~/{1}s/{0}/Widget.cshtml",
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
FileExtensions = new[] {
"cshtml"
};
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return new RazorView(controllerContext, partialPath,
layoutPath: null, runViewStartPages: false, viewStartFileExtensions: FileExtensions, viewPageActivator: ViewPageActivator);
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
var view = new RazorView(controllerContext, viewPath,
layoutPath: masterPath, runViewStartPages: true, viewStartFileExtensions: FileExtensions, viewPageActivator: ViewPageActivator);
return view;
}
}
转自:http://hi.baidu.com/makebgf/blog/item/0ff47b146e3cb5daaf513312.html