ASP Net Web Api HttpPost失败请求
我最近从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}
事实证明,Web服务器(负载均衡)重定向我的帖子作为get(因此请求为空)。将网址更改为https并且工作正常。
@Bilpor感谢您的帮助。
在邮递员JSON响应, 400错误的请求 { “消息”:“请求缺少” } –
您能不能告诉请求的构造后调用之前。 – bilpor
嗨Bilpor, 不幸的是我不能因数据敏感。我只能说json字符串是有效的,因为我使用了site [link](http://json.parser.online.fr/)。 –