S Osborn S Osborn - 2 months ago 16
ASP.NET (C#) Question

ASP Net Web Api HttpPost losing request

I've recently changed from httpget to httppost so I can put a valid json string into the body of my request. Now when I send the request either running the api locally or by publishing to my local IIS everything works as I'd expect it to. However, as soon as I deploy to our development web server the "request" gets lost and hits the line of code, return BadRequest("Request is missing");.

Here is the code from my controller,

[RoutePrefix("MyPrefix")]
public class InsightController : ApiController
{
[Route("Load"), AcceptVerbs("GET", "POST"), HttpPost, ResponseType(typeof(Logic))]
public IHttpActionResult Load([FromBody]MasterRequest request)
{
try
{
if (!ModelState.IsValid)
return BadRequest(ModelState);

if (request.IsNull())
{
return BadRequest("Request is missing");
}
...


The WebApiConfig code looks like this,

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
config.Services.Insert(typeof(ModelBinderProvider), 0, new SimpleModelBinderProvider(typeof(MasterRequest), new RequestModelBinder()));
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
...


Web.Config,

<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
...


Header content-type is set correctly to application/json and called with raw json via postman.

Can anyone point out what I've missed?

Thanks.

JSON...
{"Max_Request_Item_Count":0,"CompanyId":999999,"NoOfMonths":12,"RequestedMonth":226,"MileageBaseId":1,"SectorId":null,"ErrorSmoothing":false,"ShowDiagnostics":false,"Identifier":null,"IdentifierId":0,"ConsumerCode":"WEB","SelectedFilterType":0,"ManufacturerIds":[5,3],"RangeIds":[],"FuelIds":[],"EngineIds":[1.6,1.9],"TransmissionIds":[],"DriveIds":[],"BodyIds":[],"PowerIds":[],"WeightIds":[],"DoorIds":[],"VersionIds":[],"CO2Ids":[],"PriceIds":[],"TrimIds":[],"SeatIds":[5],"SectorIds":[],"DateAddedIds":[200,226],"DateOffProduction":"2017-08-01T14:25:09.575305+01:00","IncludeOldModels":true,"IncludedText":"no text","ExcludedText":"no text","Extrapolate":false,"Variables":{},"ElementTypeId":3,"ElementSubTypeId":8,"Created":"2017-08-11T14:25:09.5683034+01:00","CreatedBy":"BURT","CountryId":10,"VehicleTypeId":0,"ManufacturerId":0}

Answer Source

It turns out that the web server (load balanced) was redirecting my post as a get (hence the reason the request was null). Changed the url to be https and it worked.

@Bilpor Thanks for help.