简体   繁体   中英

ASP.NET Web API “400 Bad Request” on POST Request When Deploying to Test Server

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, ie 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:

Loaclhost响应标头

This is the response headers on the the test server:

测试服务器响应头

And the TextView on the test server:

测试服务器响应文本视图

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.

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' }
        })

    };
}

})();

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;
                }
            });
    }

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
{
}

What could be causing this error?

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.

Things are working now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM