我在设计视图上使用一些静态HTML和后面的代码制作了一个简单的web表单:Page_Load和Page_PreRender,如下所示:
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()呢?
发布于 2015-12-31 08:50:48
在页面中覆盖IHttpHandler的ProcessRequest(HttpContext)就足够了。
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();
}
}https://stackoverflow.com/questions/34539504
复制相似问题