gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:15:49 25 4
gpt4 key购买 nike

所以我只是在我的 MVC3 站点上实现了一个基本 Controller ,以便在加载每个 View 之前执行一些操作。特别是我想要一些可以充当母版页代码的东西。一旦我滚动它并使我的所有 Controller 都继承 self 的基本 Controller ,我就会收到此错误:

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

我阅读了一些您需要重写 CreateTempDataProvider() 并返回某些内容或其他内容的地方,但我不太明白这样做的代码。我是 .NET 的新手,之前还没有重写任何东西,所以我不确定该怎么做。如果您以前遇到过这种情况,或者知道该怎么做,请提供帮助!

这是我的基本 Controller 和覆盖我迄今为止所做的无济于事的尝试:

 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();
}
}

这是在我开始调试时尝试运行的 Controller (不知道您是否需要它):

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());
}
...
}

最佳答案

您需要在重写的 Initialize() 方法顶部调用 base.Initialize() 来初始化 Controller 基类。

否则,永远不会设置 PossiblyLoadTempData() 使用的属性。

关于c# - 覆盖 CreateTempDataProvider() 以解决 System.Web.Mvc.Controller.PossiblyLoadTempData() 处的空引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10973901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com