ASP.NET Web API"400错误请求"部署到测试服务器时对POST请求的了解 [英] ASP.NET Web API "400 Bad Request" on POST Request When Deploying to Test Server

查看:42
本文介绍了ASP.NET Web API"400错误请求"部署到测试服务器时对POST请求的了解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单页应用程序(SPA),其中AngularJS作为前端,.NET Web API作为后端.一切在我的开发机器上都可以正常运行,即当我在本地主机下的Visual Studio(2015)中运行它时.但是,一旦发布到我们的测试服务器,将POST请求发送到Web API时,我会收到"400 Bad Request"错误.GET请求工作正常.我正在用Fiddler调试它,当我在TextView选项卡中查看时,显示底层提供程序无法打开".这是Fiddler的一些屏幕截图:

I have a Single Page Application (SPA) with AngularJS as my front-end and .NET Web API as my backend. Everything works fine on my development machine, i.e. when I run it from Visual Studio (2015) under localhost. However, once published to our testing server, when sending POST requests to the Web API I get a "400 Bad Request" error. GET requests work fine. I am debugging it with Fiddler and when I look in the TextView tab it says "The underlying provider failed to Open". Here are some screenshots from Fiddler:

这是请求和响应在我的本地计算机上的样子:

This is how the request and response look on my local machine:

这是测试服务器上的响应标头:

This is the response headers on the the test server:

以及测试服务器上的TextView:

And the TextView on the test server:

通过POST请求发送的数据对于localhost和测试服务器都是相同的.同样,对于它们两个,都存在一个授权标头.除了"Referer"和"Host"的值外,我注意到它们之间的唯一其他区别是,本地主机在IIS/10.0上运行,而测试服务器在IIS/8.0上.

The data being sent through the POST request is the same for both the localhost and the test server. Also, for both of them an authorization header is present. Other than the values for "Referer" and "Host", the only other difference I am noticing between them is that localhost is running on IIS/10.0 while the test server is on IIS/8.0.

调用WebAPI的AngularJS资源的代码如下:

The code for the AngularJS resource which calls the WebAPI is the following:

(function () {
"use strict";

angular
    .module("mainApp")
    .factory("incidentResource", ["$resource", "baseApiUrl", incidentResource]);

/// The factory function. The Angular $resource service and the appSettings
/// constant are injected as parameters.
function incidentResource($resource, baseApiUrl) {

    return {

        generateFile: $resource(baseApiUrl + "/api/Imports/PreviewOverlay", null,
        {
            'generate': { method: 'POST' }
        })

    };
}

})();

这是从Angular代码中调用的,就像这样:

This is called from Angular code like so:

vm.generate = function () {
        incidentResource.generateFile.generate(vm.divisionImports,
            function (data) {
                vm.successMessage.show = true;
            },

            function (response) {
                vm.message = response.statusText + "\r\n";
                if (response.data.exceptionMessage) {
                  vm.message += response.data.exceptionMessage;
                }
            });
    }

最后,我的Web API控制器如下所示:

And finally, my Web API controller looks like this:

[RoutePrefix("api/Imports")]
public class ImportController : BaseController
{
    [Authorize]
    [HttpPost]
    [Route("PreviewOverlay")]
    public IHttpActionResult GenerateFile(DivisionImport[] chargedIncidents)
    {
        try
        {
            // Get the name of the user currently logged in
            string UserName = this.GetCurrentUserIdFromRequest();

            List<DivisionImport> incidentsList = new List<DivisionImport>();

            incidentsList.AddRange(chargedIncidents);

            this.incidentFileBuilder.GenerateFile(FileType.Delimited, incidentsList);

            return this.Ok();
        }
        catch (Exception ex)
        {
            return this.BadRequest(ex.Message);
        }
    }
}

[EnableCors(origins: "*", headers: "*", methods: "*")]
public class BaseController : ApiController
{
}

什么可能导致此错误?

推荐答案

正如@Dan Jul指出的那样,该错误是由错误的数据库连接字符串引起的.在将代码部署到测试服务器时,我们将连接配置文件更改为另一个文件(用于测试服务器),并且其中包含带有语法错误的连接字符串.

As @Dan Jul pointed out, the error was caused by a faulty database connection string. While deploying my code to our test server, we changed the connection configuration file to a different one (for the test server) and it contained a connection string with a syntax error.

一切正常.

这篇关于ASP.NET Web API"400错误请求"部署到测试服务器时对POST请求的了解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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