【问题标题】:ASP Net Web Api HttpPost losing requestASP Net Web Api HttpPost 丢失请求
【发布时间】:2017-08-14 08:56:18
【问题描述】:

我最近从 httpget 更改为 httppost,因此我可以将有效的 json 字符串放入我的请求正文中。现在,当我在本地运行 api 或通过发布到本地 IIS 发送请求时,一切都按我的预期工作。但是,一旦我部署到我们的开发 Web 服务器,“请求”就会丢失并到达代码行,返回 BadRequest(“Request is missing”);。

这是来自我的控制器的代码,

[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,"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,"变量":{},"ElementTypeId":3,"ElementSubTypeId":8,"Created":"2017-08-11T14:25:09.5683034+01:00","CreatedBy":"BURT"," CountryId":10,"VehicleTypeId":0,"ManufacturerId":0}

【问题讨论】:

  • 邮递员中的 Json 响应,400 Bad Request { "message": "Request is missing" }
  • 你能在帖子被调用之前显示请求的构造吗?
  • 嗨,Bilpor,很遗憾,由于数据敏感,我不能。我只能说json字符串是有效的,因为我使用了站点link
  • POST /api/MyPrefix/load HTTP/1.1 主机:dev.com 内容类型:application/json 缓存控制:无缓存 邮递员令牌:fb584bf8-53ea-a2c4-b13b-5bcba1dade98
  • json 字符串太大,无法在此处发布,因此我已添加到上述原始问题的底部。

标签: c# asp.net http-post asp.net-web-api2


【解决方案1】:

事实证明,Web 服务器(负载平衡)将我的帖子重定向为 get(因此请求为空)。将网址更改为 https 并且有效。

@Bilpor 感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2016-09-22
    • 2021-11-03
    • 2017-07-10
    • 2017-03-16
    • 2014-01-14
    • 2022-10-19
    • 2021-11-10
    • 2019-01-19
    • 2019-04-21
    相关资源
    最近更新 更多