2017-08-14 71 views
0

我最近從httpget更改爲httppost,因此我可以將有效的json字符串放入我的請求的正文中。現在,當我發送請求時,無論是在本地運行api還是發佈到本地IIS,一切都按照我預期的那樣工作。但是,一旦我部署到我們的開發Web服務器,「請求」就會丟失,並碰到代碼行,返回BadRequest(「Request is missing」);.ASP Net Web Api HttpPost失敗請求

這裏是我的控制器代碼,

[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"); 
      } 
... 

的WebApiConfig代碼看起來像這樣,

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> 
... 

頁眉內容類型設置正確的應用程序/ json和通過郵遞員與原始json調用。

任何人都可以指出我錯過了什麼?

謝謝。

JSON ... { 「Max_Request_Item_Count」:0 「CompanyId」:999999 「NoOfMonths」:12, 「RequestedMonth」:226, 「MileageBaseId」:1, 「扇區ID」:NULL, 「ErrorSmoothing」:假, 「ShowDiagnostics」:假, 「標識符」: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」: 「無文本」,「外推」:假,「變量」:{},「ElementTypeId」:3,「ElementSubTypeId」:8,「創建」:「2017-08-11T14:25:09.5683034 + 01:00」, 「CreatedBy」:「BURT」,「CountryId」:10,「VehicleTypeId」:0,「ManufacturerId」:0}

+0

在郵遞員JSON響應, 400錯誤的請求 { 「消息」:「請求缺少」 } –

+0

您能不能告訴請求的構造後調用之前。 – bilpor

+0

嗨Bilpor, 不幸的是我不能因數據敏感。我只能說json字符串是有效的,因爲我使用了site [link](http://json.parser.online.fr/)。 –

回答

0

事實證明,Web服務器(負載均衡)重定向我的帖子作爲get(因此請求爲空)。將網址更改爲https並且工作正常。

@Bilpor感謝您的幫助。