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

…衆ロ難τιáo~ 提交于 2019-12-11 04:52:00

问题


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:

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?


回答1:


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.



来源:https://stackoverflow.com/questions/38571183/asp-net-web-api-400-bad-request-on-post-request-when-deploying-to-test-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!