在 main.js 我添加了行:
viewLocator.useConvention('viewmodels', 'ViewsProxy/GetView?name=', 'ViewsProxy/GetView?name=');
并实现 ViewsProxyController 如下:
public class ViewsProxyController : Controller
{
public ActionResult GetView(string name)
{
string viewRelativePath = GetRelativePath(name);
string viewAbsolutePath = HttpContext.Server.MapPath(viewRelativePath);
if (!System.IO.File.Exists(viewAbsolutePath))
{
viewAbsolutePath = ReplaceHtmlWithCshtml(viewAbsolutePath);
if (System.IO.File.Exists(viewAbsolutePath))
{
System.Web.HttpContext.Current.Cache.SetIsHtmlView(name, false);
viewRelativePath = ReplaceHtmlWithCshtml(viewRelativePath);
return PartialView(viewRelativePath);
}
throw new FileNotFoundException();
}
FilePathResult filePathResult = new FilePathResult(viewAbsolutePath, "text/html");
return filePathResult;
}
private string ReplaceHtmlWithCshtml(string path)
{
string result = Regex.Replace(path, @"\.html$", ".cshtml");
return result;
}
private string GetRelativePath(string name)
{
string result = string.Format("{0}{1}", AppConfigManager.DurandalViewsFolder, name);
return result;
}
}
所以现在 durandal 应用程序可以使用 .html 和 .cshtml。