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

标签 c# .net web-services iis

我设置了一个 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/

相关文章:

Java WSDL 修改或隐藏 Web View 上的某些属性

c# - Unity无法旋转粒子系统

.net - .NET Framework 中是否支持将 .NET 数据类型映射到其对应的 SqlDBType?

c# - .NET 序列化 XmlNode 问题

c# - 动态禁用特定的上下文菜单项

asp.net - 尝试使 Web 方法异步

c# - 如何修复此 ArrayIndex 错误?

c# - DDD 中的每个服务类都有一个接口(interface)是一种好的设计实践吗?

c# - 禁用网络适配器时获取MAC地址?

java - pd4ml 的 Maven 依赖项