设置Context.Response.StatusCode似乎不起作用

时间:2010-07-22 13:17:09

标签: c# .net http http-status-codes

我有一个带有以下代码的HttpHandler:

using System;
using System.Web;
using Company.Cms;
using Company.Web.Handlers.Console;


namespace Company.Web.Handlers
{
    /// <summary>
    /// Summary description for AdminHandler
    /// </summary>
    public class AdminHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string action = request.QueryString["action"];

            if (!HttpContext.Current.User.CanAdminister())
            {
                response.StatusCode = 403;
                response.Status = "403 Access Denied";
                response.End();
                return;
            }

            if (String.IsNullOrEmpty(action))
            {
                response.StatusCode = 404;
                response.Status = "404 Not Found";
                response.End();
                return;
            }

            IHttpHandler handler = null;
            switch (action)
            {
                case "menu":
                    handler = new MenuHandler();
                    break;
                case "tree":
                    handler = new TreeHandler();
                    break;
                case "grid":
                    handler = new GridHandler();
                    break;
                case "list":
                    handler = new ListHandler();
                    break;
                case "drop":
                    handler = new DropHandler();
                    break;
                case "edit":
                    handler = new EditHandler();
                    break;
                case "new":
                    handler = new InsertHandler();
                    break;
            }
            if (handler == null)
            {
                response.StatusCode = 404;
                response.Status = "404 Not Found";
                response.End();
            }
            else
            {
                handler.ProcessRequest(context);
            }
        }
    }
}

不幸的是,当我故意指定无效操作时,浏览器只显示一个空白页面。在Firefox和IE中都不显示浏览器错误消息。

我可能做错了什么?

编辑 - IE显示错误消息,但Firefox没有。

4 个答案:

答案 0 :(得分:13)

首先尝试一下:

protected void Page_Load(object sender, EventArgs e)
{
    Response.StatusCode = 404;
    Response.SuppressContent = true;
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

:)〜

答案 1 :(得分:8)

  

Firebug显示正确的状态。是否   这意味着,如果我想要浏览器   要显示一条消息,我必须渲染   我自己? - deverop

绝对是这样的。浏览器根据收到的错误代码执行的操作取决于浏览器。但是你仍然可以提供HTML来与404一起使用。例如......看看Stack Overflow's 404 page。该错误消息完全是手工制作的。

但是,通常,您希望限制从错误状态返回的数据量;您从错误请求返回的数据越多,拒绝服务的攻击面就越大。

答案 2 :(得分:3)

我遇到了类似的问题,仅在IIS 7.0中发生。您还可以尝试设置

Response.TrySkipIisCustomErrors = true;

答案 3 :(得分:0)

对于301重定向,请使用server <- function(input, output, session) { Test <- reactive({ if(input$Country == 'All') { Joined_data %>% filter(`Contract Locality` == input$Locality) } else { Joined_data %>% filter(`Country` == input$Country, `Region` == input$Region) }}) output$Total <- renderValueBox({ valueBox(Test() %>% tally(), req(input$Country) }) output$Value <- renderValueBox({ valueBox(Test() %>% summarise("Annualised_Value" = sum(`Value (Annualised)`)) %>% prettyNum(big.mark = ","), req(input$Country) }) Country.choice <- reactive({ Joined_data %>% filter(Country == input$Country %>% pull(Region) }) observe({ updateSelectizeInput(session, "Region", choices = Country.choice()) }) } shiny::shinyApp(ui=ui,server=server)

对于302重定向,请使用Response.RedirectPermanent()

Response.Redirect()