我最近从httpget更改为httppost,因此我可以将有效的json字符串放入我的请求正文中。现在,当我发送请求时,无论是在本地运行api还是通过发布到我的本地IIS,一切都按照我的预期运行。但是,一旦我部署到我们的开发Web服务器,“请求”就会丢失并触及代码行,返回BadRequest(“请求丢失”);.
以下是我的控制器的代码
[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>
...
标题内容类型已正确设置为application / 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”:[200226 ],“DateOffProduction”:“2017-08-01T14:25:09.575305 + 01:00”,“IncludeOldModels”:true,“IncludedText”:“no text”,“ExcludedText”:“no text”,“Extrapolate”:假 “的变量”:{}, “ElementTypeId”:3 “ElementSubTypeId”:8, “创建”: “2017-08-11T14:25:09.5683034 + 01:00”, “CreatedBy”: “BURT”,” CountryId “:10”,VehicleTypeId “:0”,ManufacturerId“:0}
答案 0 :(得分:0)
事实证明,Web服务器(负载平衡)将我的帖子重定向为get(因此请求为空的原因)。将网址更改为https并且有效。
@Bilpor感谢您的帮助。