我在 VS2012 上获得了 DurandalJS StarterKit 模板...一切都很好...
但在某些观点中我需要做这样的事情:
@if (Roles.IsUserInRole("Administrators"))
{
<p>Test</p>
}
然而,对于 durandal,我的所有视图都是“.html”文件...是否可以使用“.cshtml”文件来访问类似的信息?
或者还有其他方法可以用 durandal 做到这一点吗?
Junior
我这样做是这样的:
-
为 Durandal 视图创建通用控制器:
public class DurandalViewController : Controller
{
//
// GET: /App/views/{viewName}.html
[HttpGet]
public ActionResult Get(string viewName)
{
return View("~/App/views/" + viewName + ".cshtml");
}
}
-
注册路线:
routes.MapRoute(
name: "Durandal App Views",
url: "App/views/{viewName}.html",
defaults: new { controller = "DurandalView", action = "Get" }
);
将 Views/web.config 复制到 /App/views/web.config (以便 Razor 视图在此位置工作)。
这让我可以使用正常的 Durandal 约定(甚至视图的 html 扩展名),并将 durandal 视图作为 cshtml 文件放在其正常位置,而无需添加任何更多服务器代码。
如果您还有静态 html 视图,您还可以将 cshtml 视图放在子文件夹中或使用普通的 MVC /Views 文件夹。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)