首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpApplication.CompleteRequest()的意义是什么?

HttpApplication.CompleteRequest()的意义是什么?
EN

Stack Overflow用户
提问于 2015-12-31 00:47:16
回答 1查看 791关注 0票数 3

我在设计视图上使用一些静态HTML和后面的代码制作了一个简单的web表单:Page_LoadPage_PreRender,如下所示:

代码语言:javascript
复制
public partial class SamplePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        Respons.Write("Not Found");
        Context.ApplicationInstance.CompleteRequest();
    }

    protected void Page_PreRender(object sender, EventArgs e) // Why does this event get called? Should not the CompleteReqeust()
    {                                                         // cause the page to jump directly to the end of events pipeline?
        throw new NotImplementedException();
    }
}

而且,我读过很多关于Response.End()“丑陋”、“危险”等问题,甚至是从MSDN网站上读到的。但是这让我很困惑,如果是这样,为什么Response.Redirect(string)仍然在内部使用Response.End()呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-31 08:50:48

在页面中覆盖IHttpHandlerProcessRequest(HttpContext)就足够了。

代码语言:javascript
复制
public partial class SamplePage : System.Web.UI.Page
{
    public override void ProcessRequest(System.Web.HttpContext context)
    {
        if (conditionTrue)
        {
            context.Response.StatusCode = 404;
            context.ApplicationInstance.CompleteRequest();
        }
        else
        {
            base.ProcessRequest(context);
        }
    }

    protected void Page_Load(object sender, EventArgs e) // This is not called also :P
    {
    }

    protected void Page_PreRender(object sender, EventArgs e) // Not called now :)
    {
        throw new NotImplementedException();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34539504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档