Web API - 使用HttpStatusCode.BadRequest的Request.CreateResponse导致内部服务器错误

时间:2014-10-30 10:40:16

标签: asp.net web-services asp.net-web-api

我的项目中存在以下代码行的问题。我在Windows 2008 R2上使用Web API,使用IIS 7.5。

return Request.CreateResponse(HttpStatusCode.BadRequest, "Bad Request");

但是,此命令可以完美运行:

return Request.CreateResponse(HttpStatusCode.OK, "Ok Response");

知道为什么会这样吗?

控制器代码:

[Throttle(Name = "ProductCtrl", Count = 10, Seconds = 10)]
public class ProductController : ApiController
{
    [HttpGet]
    [Authorize]
    [ActionName("Current-ProductName")]
    public HttpResponseMessage GetCurrentProductName(string id)
    {
      // I added this code to test also but I can see the log but it return  
      // Internal server error instead  Bad Request
       Log.Info("Test");
       return Request.CreateResponse(HttpStatusCode.BadRequest, "Bad Request");

        try
        {

            //Codes to get the data .....

            var productName = "My productName";
            return Request.CreateResponse(HttpStatusCode.OK, productName);

        }
        catch (Exception e)
        {
            //Log the Errors...

            return Request.CreateResponse(HttpStatusCode.BadRequest, "Bad Request");
        }

    }


}
WebApi.Config中的

config.Routes.MapHttpRoute(
            name: "ProductCtrlWithAction",
            routeTemplate: "api/productInfo/{action}/{id}",
            defaults: new { controller = "Product", action = "All", id = RouteParameter.Optional }
        );

来自Fiddler的电话 /原料药/ ProductInfo /电流产品名称/ 1001

1 个答案:

答案 0 :(得分:0)

我有同样的问题。当我将返回值更改为return BadRequest();时,它再次起作用。