简体   繁体   English

MVC WebAPI 调用一次且仅一次

[英]MVC WebAPI Call Works Once and Only Once

I have a WebAPI deployed as a Azure Web App It is .net Framework 4.6.2.我有一个部署为 Azure Web 应用程序的 WebAPI 它是 .net Framework 4.6.2。 I can call it it once (from a variety of sources, including my JavaScript code and Postman) but the second time it gets called I get the following error:我可以调用它一次(来自各种来源,包括我的 JavaScript 代码和 Postman),但第二次调用它时出现以下错误:

{ "Message": "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.", "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'ImportSourceLineActionRequest' from content with media type 'application/octet-stream'.", "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException", "StackTrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)" } { "Message": "请求包含一个实体主体,但没有 Content-Type header。此资源不支持推断的媒体类型 'application/octet-stream'。", "ExceptionMessage": "没有可读取的 MediaTypeFormatter来自媒体类型为“application/octet-stream”的内容的“ImportSourceLineActionRequest”类型的 object。 System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage 请求,IFormatterLogger formatterLogger,CancellationToken cancellationToken)格式器,IFormatterLogger formatterLogger,CancellationToken cancellationToken) }

Some things to note:一些注意事项:

  • The Content-Type is set in Postman to "application/json" Content-Type 在 Postman 中设置为“application/json”
  • The Content-Type is set in my JavaScript to "application/json" Content-Type 在我的 JavaScript 中设置为“application/json”

So... why does my webservice suddenly think I am sending application/octet-stream?那么...为什么我的网络服务突然认为我正在发送应用程序/八位字节流?

Further, rebooting the Web App fixes the issue... for one more time.此外,重新启动 Web 应用程序修复了问题……再一次。

Javascript code making the call: Javascript 拨打电话的代码:

 const resp = await fetch(callUrl, { method: 'POST', body: bodyOjbectStr, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } });

API Method that is getting called API 被调用的方法

[HttpPost]
public void DoSomeWork(DoSomeWorkRequestRequest request)
{
    // Do various things
}

WebApiConfig.cs (snippet of) WebApiConfig.cs(的片段)

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // config.Filters.Add(new RequireHttpsAttribute());

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

            GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new System.Net.Http.Formatting.RequestHeaderMapping("Accept","text/html",StringComparison.InvariantCultureIgnoreCase,true,"application/json"));
        }
    }

Any help gratefully accepted.感激地接受任何帮助。

DS DS

For completeness, this problem got solved.为了完整起见,这个问题得到了解决。 It turned out to be a problem with the 2FA we were using.结果证明是我们使用的 2FA 有问题。 Not sure why it ever worked once and failed the second time, but when IT made some adjustments to 2FA it all worked.不确定为什么它曾经工作一次而第二次失败,但是当 IT 对 2FA 进行一些调整时,一切都奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM