简体   繁体   English

覆盖CreateTempDataProvider()以解决System.Web.Mvc.Controller.PossiblyLoadTempData()中的空引用异常

[英]Override CreateTempDataProvider() to solve null reference exception at System.Web.Mvc.Controller.PossiblyLoadTempData()

So I just implemented a base controller on my MVC3 site in order to have some things execute before every view is loaded. 所以我只是在我的MVC3站点上实现了一个基本控制器,以便在加载每个视图之前执行一些操作。 Particularly I wanted something that would act as a kind of master page code behind. 特别是我想要的东西可以作为一种主页面代码。 Once I rolled this and made all my controllers inherit from my base controller I get this error: 一旦我滚动它并使我的所有控制器继承自我的基本控制器,我得到这个错误:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.



[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.Controller.PossiblyLoadTempData() +11
System.Web.Mvc.Controller.ExecuteCore() +38
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

I read a few places that you need to override CreateTempDataProvider() and return something or other, but I can't quite figure out the code to do so. 我读了一些你需要覆盖CreateTempDataProvider()并返回其他东西的地方,但我无法弄清楚这样做的代码。 I am relatively new with .NET and have yet to override anything before, so I am not sure how to go about this. 我对.NET比较新,以前还没有覆盖任何东西,所以我不知道该怎么做。 IF you have run into this before, or know what to do, please help! 如果您之前遇到过这种情况,或者知道该怎么做,请帮忙!

Here is my base controller and the attempt at overriding that I have done so far to no avail: 这是我的基本控制器,并尝试覆盖我迄今为止所做的无济于事:

 public class BaseController : Controller
{
    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {

        string document = Path.Combine(HttpRuntime.AppDomainAppPath, "quote.xml");
        string _quote = "";
        string _author = "";
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(document);

        XmlNode Quote = xmlDoc.SelectSingleNode("quote/text");
        XmlNode Author = xmlDoc.SelectSingleNode("quote/author");

        _quote = Quote.InnerText;
        _author = Author.InnerText;

        ViewData["Quote"] = _quote;
    }

    protected override ITempDataProvider CreateTempDataProvider()
    {
        return base.CreateTempDataProvider();
    }
}

And here is the controller that is trying to run when I start debugging (dunno if you need it): 这是我开始调试时尝试运行的控制器(如果需要,则为dunno):

public class BlogController : BaseController
{
    private DarkRobotEntities1 db = new DarkRobotEntities1();

    //
    // GET: /Blog/

    public ViewResult Index()
    {
        var posts = db.Posts.Include(p => p.Blog).Include(p => p.Comments);
        return View(posts.ToList());
    }
...
}

You need to call base.Initialize() at the top of your overridden Initialize() method to initialize the base Controller class. 您需要在重写的Initialize()方法的顶部调用base.Initialize()来初始化基本Controller类。

Otherwise, the properties used by PossiblyLoadTempData() will never be set. 否则, PossiblyLoadTempData()使用的属性将永远不会被设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM