1

我有一个ASP.Net MVC 4,与捆绑.net 4.5项目。当我将[Authorize]添加到我的Controller时,@ Scripts.Render和@ Styles.Render调用与“对象引用未设置为对象实例”打破。授权属性break @ Script.Render

我不能为我的生活工作出为什么!

有没有人遇到过这个?谁能帮忙?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Project.Models; 
using Project.Repositories; 

namespace Project.Controllers 
{ 
    [Authorize] 
    public class HomeController : Controller 

这打破我_layout观点:

@model PageModel 
<!DOCTYPE html> 
<!--[if lt IE 7]>  <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> 
<!--[if IE 7]>   <html class="no-js lt-ie9 lt-ie8"> <![endif]--> 
<!--[if IE 8]>   <html class="no-js lt-ie9"> <![endif]--> 
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 

    <title>Project</title> 
    <meta name="description" content="">  
    <meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0'/><meta name="apple-mobile-web-app-capable" content="yes" /> 
    <meta name="format-detection" content="telephone=no"> 


    @Styles.Render("~/Content/bootstrap") 
    @Scripts.Render("~/bundles/jquery")   
    @Scripts.Render("~/bundles/initializr")   
+0

你有没有试过调试器?你在哪个对象上得到异常?向我们展示一些代码 – Dmitry 2013-03-14 17:32:40

+0

嗨德米特里,我试过调试,但内部异常为空,堆栈跟踪显示没有用处......我无所适从。 – Doga 2013-03-14 17:40:51

回答

1

右键,抓我的头对这一小时,在寻找各种不相关的地方之后,我摸索出了问题......

我的_Layout视图有一些其他的剃刀调用,它们在下方查看pagemodel属性,但是当我重定向登录进行认证时,pagemodel为null,这就是问题所在。

只记得乡亲们,Visual Studio打破的路线并不总是与实际问题一致,特别是在剃刀上,应该早早记住这一点!在下一个声明中看下去。

1

我以前经历过这个,我发现我的错误。问题是,在这条线@Styles.Render("~/andia-agency/css")(_layout)

在BundleConfig文件

  bundles.Add(
      new ScriptBundle("~/andia-agency/css") 
      .Include("~/Content/andia-agency/assets/bootstrap/css/bootstrap.min.css") 
      .Include("~/Content/andia-agency/assets/prettyPhoto/css/prettyPhoto.css") 
      .Include("~/Content/andia-agency/assets/css/flexslider.css") 
      .Include("~/Content/andia-agency/assets/css/font-awesome.css") 
      .Include("~/Content/andia-agency/assets/css/style.css")); 

我改变了新ScriptBundleStyleBundle。这可能会解决您或其他人的问题。

2

我只是想为此添加我的修复程序。我在最后一个小时与它搏斗,终于意识到它是什么。就我而言,@Scripts.Render("~/my/script/bundle")包含一个JavaScript文件,其中包含一个简单的语法错误。而已。当我意识到这可能是什么时,我去看了一下JavaScript文件,果然,一条红色的波浪线给了我一个手指。