gpt4 book ai didi

asp.net - Azure 网站上的 REST API 上的 400.0 错误请求

转载 作者:行者123 更新时间:2023-12-03 03:17:30 24 4
gpt4 key购买 nike

我已将 REST 服务迁移到 Azure 网站,但它总是返回 400.0 Bad Request 错误(错误代码为零)。

该服务在本地运行,并且在 Web 角色中运行时运行良好。进入网站后,它开始返回 400.0 错误。

我的 web.config:

<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>

我已经在我的类中设置了 ASP.NET 兼容性:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class BookingService { ... }

以及 Global.asax.cs 中定义的正确路由:

private void Application_Start (object sender, EventArgs e) {
RouteTable.Routes.Add(new ServiceRoute("booking/service", new WebServiceHostFactory(), typeof(BookingService)));
}

现在,当我尝试在本地访问 REST API 时,它工作正常:

http://localhost/booking/service/help

但是,将 Web 服务上传到 Azure 网站并执行以下操作后:

http://xxx.azurewebsites.net/booking/service/help

它返回了 400.0 错误请求。详细错误日志显示:

Request: ManagedPipelineHandler
Notification: ExecuteRequestHandler
Handler: System.ServiceModel.Activation.AspNetRouteServiceHttpHandler
Error code: 0x00000000

我完全迷失了。谷歌搜索网络没有产生任何有用的结果。我尝试将以下内容放入 web.config 中:

<handlers>
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="Foo.RoutingHandler" />
</handlers>

并通过扩展UrlRoutingHandler并定义VerifyAndProcessRequest来定义处理程序,但它仍然不起作用。

最佳答案

把服务一点点剥离后终于发现了错误。

发生的事情是:

  • RouteTable.Routes.Add 添加一个路由,该路由创建 BookingService 类的对象作为处理程序
  • BookingService 有一个已初始化的静态属性
  • 此初始化语句失败(由于某种原因此处未提及)
  • 因此,当路由服务尝试创建 BookingService 类时,该类的静态初始值设定项首先运行并失败
  • 因此,代码从未到达路由服务,因此服务在 AspNetRouteServiceHttpHandler 处理程序处失败
  • 如果类不是在运行静态初始化程序时失败,而是在对象创建期间失败,则路由服务很可能会抛出一个更有意义的错误来指出问题

总之,如果您在处理程序中看到路由请求失败,则可能是由于无法加载类代码造成的(由于静态初始化器失败或其他原因)。

关于asp.net - Azure 网站上的 REST API 上的 400.0 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com