有没有办法阻止脚本包在特定 View 上渲染?我想在大多数 View 上默认使用 _layout 页面及其脚本包。我在使用 jQuery 网格并在该 View 中包含了我需要的脚本的特定 View 中遇到了问题。布局页面中添加的 bundle 打破了我对网格的看法。
最佳答案
您可以通过ViewContext.RouteData
访问返回View的Action的名称。
查看:
~/Views/MyViews/MyAction.cshtml
行动:
public ActionResult MyAction()
{
return View();
}
以下是从 View 内部访问操作名称的方法:
string currentAction = ViewContext.RouteData["action"].ToString();
并在渲染 Bundle 之前检查它:
@if(currentAction != "MyAction")
{
@Scripts.Render(/* your script bundle here... */)
}
为了进一步完善这一点,如果需要,您可以使用类似的方法来检查 Controller 的名称。
希望这有帮助。
关于jquery - MVC 4 阻止脚本包在特定 View 上渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20108740/