我正在使用IIS 7.5后端构建MVC 3应用程序.在我的控制器上,我有一些允许用户添加/编辑域对象的操作方法.Action处理HTTP Post,其返回值为string,其中包含在保存过程中遇到的任何验证错误消息.以下是一个Action方法的示例:
[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)
{ …Run Code Online (Sandbox Code Playgroud) 我正在编写Web API服务,如果我的ModelState无效,则尝试返回(400)错误请求.我不希望将响应主体附加到此.似乎IIS正在劫持我的响应,并且总是返回带有冗长的样式错误页面的text/html内容类型.这是个问题.
[HttpPost]
public void Link(LinkDeviceModel model)
{
if (ModelState.IsValid)
{
try
{
model.Save();
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
throw new HttpResponseException(ex.Message, HttpStatusCode.InternalServerError);
}
}
else
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的小提琴请求:
POST http://localhost/myapp/service/link HTTP/1.1
Host: localhost
Content-Length: 112
Content-Type: application/json
Accept: application/json
{"DeviceUniqueId":"CC9C6FC0-7D06-11E1-8B0E-31564824019B", "UserName": "me@mycompany.com"," Pin": "111111"}
Run Code Online (Sandbox Code Playgroud)
我的回答是错误的,充满了身体,回应:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>IIS 7.5 Detailed Error - 400.0 - Bad Request</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;background:#CBE1EF;}
code{margin:0;color:#006600;font-size:1.1em;font-weight:bold;}
.config_source code{font-size:.8em;color:#000000;}
pre{margin:0;font-size:1.4em;word-wrap:break-word;}
ul,ol{margin:10px …Run Code Online (Sandbox Code Playgroud)