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文件,果然,一條紅色的波浪線給了我一個手指。