【问题标题】:Custom Error Handlers throwing 405 for asmx WebService为 asmx WebService 引发 405 的自定义错误处理程序
【发布时间】:2015-08-12 15:03:43
【问题描述】:

我已经设置了一个 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 开发机器上做同样的事情。

【问题讨论】:

    标签: c# .net web-services iis


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-12-29
      • 2017-12-18
      • 1970-01-01
      • 2012-08-08
      • 2014-12-01
      • 2018-04-29
      • 2011-06-01
      • 1970-01-01
      • 2011-04-21
      相关资源
      最近更新 更多