当编译调试为false时,HttpContext.IsPostNotification为false

时间:2015-03-05 12:12:43

标签: c# asp.net-mvc asp.net-mvc-4

在MVC项目中,当

时,HttpContext.IsPostNotification为false
<compilation debug="false" targetFramework="4.5" />

时,HttpContext.IsPostNotification为true
<compilation debug="true" targetFramework="4.5" />

我的控制器“HomeController.cs”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Learning.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            ViewBag.PostNotification = HttpContext.IsPostNotification;
            return View();
        }
    }
}

我的观点“Index.cshtml”

@{
    ViewBag.Title = "Home Page";
}
@ViewBag.PostNotification

MVC应用程序在Windows Server(在Windows 7中开发),IIS7集成管道模式中运行。

我想将我的应用程序移至生产环境,但设置为

时HttpContext.IsPostnotification为false
<compilation debug="false" targetFramework="4.5" />

1 个答案:

答案 0 :(得分:1)

在动作方法中使用HttpContext.IsPostNotification毫无意义。 IsPostNotification旨在与HttpContext.CurrentNotification to distinguish between Main and Post HttpApplication Events一起使用。

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ...
    }

    // Main Event handler
    void Application_AcquireRequestState(object sender, EventArgs e)
    {
        //Context.CurrentNotification Is "AcquireRequestState"
        //Context.IsPostNotification Is "False"
    }

    // Post Event handler
    void Application_PostAcquireRequestState(object sender, EventArgs e)
    {
        //Context.CurrentNotification Is "AcquireRequestState"
        //Context.IsPostNotification Is "True"
    }

    ...
}

我不确定您要尝试实现的目标,但如果您想检测HttpApplication事件完成,那么您可以将代码放入事件处理程序中,除非此事件handler已在其他事件中注册,然后您需要使用CurrentNotificationIsPostNotification来检测哪个事件触发了处理程序。