gpt4 book ai didi

c# - 为 asmx WebService 抛出 405 的自定义错误处理程序

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:20 25 4
gpt4 key购买 nike

我设置了一个 Web 服务(.asmx 类型),但在打开自定义错误处理时遇到了问题;关闭自定义错误后一切正常。当我打开它们并进行相同的 Web 服务调用时,我返回 405 状态并出现以下错误:

[HttpException (0x80004005): Request format is unrecognized.]
System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +489467
System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +120
System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +346
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

我的 web 服务声明如下,它通过 javascript 作为 POST 调用到 http://xxx/WebServices/TestService.asmx/MyMethod正文中有 param1=xyz。

namespace Website.WebServices
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class TestService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public void MyMethod(string param1)
{
//...Do some important stuff
}
}
}

为了完整起见,我在 Web.Config 中的声明如下:

<system.webServer>
...
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1"/>
<error statusCode="404" path="/Error404.aspx" responseMode="ExecuteURL"/>
<remove statusCode="500" subStatusCode="-1"/>
<error statusCode="500" path="/Error.aspx" responseMode="ExecuteURL"/>
</httpErrors>
...
</system.webServer>

在 Application_BeginRequest 全局函数中执行此操作后,我可以看到一切正常 - 我只是看不出可能哪里出错了!我也直接转到 asmx 文件并通过生成的表单提交并收到相同的错误。

这已经过测试,并且在 IIS7 机器和我的 IIS express 开发机器上做同样的事情。

最佳答案

设法通过将 existingResponse 更改为 Auto 来解决此问题(感谢 Merk 的回答 Response.TrySkipIisCustomErrors not working)。

我的理解是,您有两种类型的错误响应; 替换PassThroughPassThrough 准确返回 .NET 生成的内容(跳过 IIS 错误),Replace 将响应替换为您在该代码中定义的任何内容。因为我将此设置为 Replace,所以它正在废弃我的 WebService 响应并将其替换为自定义响应,由于 runAllManagedModulesForAllRequests 标志被设置为 true,它试图重新 -运行 web 服务调用但没有请求主体 - 因此原始错误 405 因为缺少 param1。

希望这可以帮助其他人节省一天的调查时间!

关于c# - 为 asmx WebService 抛出 405 的自定义错误处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31968958/

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