4

そのため、すべてのビューが読み込まれる前にいくつかのことを実行するために、MVC3 サイトに基本コントローラーを実装しました。特に、一種のマスター ページ コード ビハインドとして機能するものが必要でした。これをロールして、すべてのコントローラーがベースコントローラーから継承されるようにすると、次のエラーが発生します。

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 に比較的慣れていないため、これまでに何かをオーバーライドしたことがないため、どうすればよいかわかりません。以前にこれに遭遇したことがある場合、または何をすべきか知っている場合は、助けてください!

これが私のベースコントローラーであり、これまでに行ったオーバーライドの試みは役に立ちませんでした:

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

そして、デバッグを開始したときに実行しようとしているコントローラーは次のとおりです(必要な場合はわかりません):

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

1 に答える 1

12

基本クラスを初期化するbase.Initialize()には、オーバーライドされたメソッドの先頭で呼び出す必要があります。Initialize()Controller

そうしないと、 で使用されるプロパティPossiblyLoadTempData()が設定されません。

于 2012-06-11T02:20:54.260 に答える