HTTP响应状态设置为400时IIS覆盖HTTP响应文本 [英] IIS Overwriting HTTP Response Text When HTTP Response Status set as 400

查看:158
本文介绍了HTTP响应状态设置为400时IIS覆盖HTTP响应文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建带有IIS 7.5后端的MVC 3应用程序.在我的控制器上,我具有允许用户添加/编辑域对象的操作方法.该Action处理HTTP Post,其返回值为string,其中包含保存过程中遇到的任何验证错误消息.这是一种操作方法"的示例:

I am building an MVC 3 application with an IIS 7.5 backend. On my controller, I have action methods that allow the user to add/edit domain objects. The Action handles HTTP Post, has a return value of string which contains any validation error messages encountered during the save process. Here is an example of one Action Method:

    [HttpPost]
    public string CustomerEdit(CustomerModel customerModel)
    {
        var errorMessages = new StringBuilder();
        var serverErrors = new List<string>();

        //Map to a customer domain object
        Mapper.CreateMap<CustomerModel, Customer>();
        var customer = Mapper.Map<CustomerModel, Customer>(customerModel);

        if (customerModel.Oper == "del")
        {
            var customerIds = new List<Guid>();
            customerIds.Add(customer.Id);

            if (!_serverService.DeleteCustomers(customerIds))
            {
                errorMessages.Append("The item could not be deleted");
                Response.StatusCode = Constants.STATUS_SERVER_ERROR;
            }
        }
        else
        {
            //Validate
            if (!_serverService.ValidateCustomer(customer, out serverErrors))
            {
                foreach (var error in serverErrors)
                {
                    ModelState.AddModelError("Validation", error);
                }
            }

            //Save
            if (ModelState.IsValid)
            {
                var saveStatus = _serverService.SaveCustomer(ref customer, _id);

                if (!saveStatus)
                {
                    errorMessages.Append("The server encountered and error during Save");
                    Response.StatusCode = Constants.STATUS_SERVER_ERROR;
                }
            }
            else
            {
                errorMessages.Append(GetValidationErrors(ModelState));
                Response.StatusCode = Constants.STATUS_SERVER_ERROR;
            }
        }
        return errorMessages.ToString();
    }

在发生错误的情况下,我需要将Response.StatusCode属性设置为400/500的值,并返回详细错误消息的串联字符串.不幸的是,IIS总是将我的错误字符串从响应测试中删除,并且(在400个错误的情况下)添加用字符串错误请求"替换它

In the case of an error, I need to set the Response.StatusCode property to a value of either 400/500, and return a concatenated string of detailed error messages. Unfortunately, IIS always strips my error string out of the response test, and (in the case of 400 errors) adds replaces it with the string 'Bad Request'

当状态码设置为400时,是否可以将IIS配置为返回自定义的,特定于操作的字符串?

Is there a way to configure IIS to return a custom, Action-specific, string when the status code is set to 400?

推荐答案

与我的一位精通IIS的朋友交谈之后,我发现在IIS 7+中,您可以将以下内容添加到web.config中:/p>

After talking to a friend of mine who is a wiz at configuring IIS, I found that in IIS 7+ you can add the following to web.config:

<system.webServer>
  <httpErrors errorMode="Detailed"/>
</system.webServer>

如果使用了web.config中的此设置,并且您设置了响应主体,则响应主体将到达客户端.如果未设置响应正文,则IIS将为详细的错误页面提供详细的错误信息(请参阅

If this setting in web.config is used, AND you set the body of the response, then the response body will reach the client. If you do NOT set the response body, then IIS will serve up a detailed error page with detailed error information (see http://learn.iis.net/page.aspx/267/how-to-use-http-detailed-errors-in-iis/). Many folks consider this a security risk, so use with caution.

这篇关于HTTP响应状态设置为400时IIS覆盖HTTP响应文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆