自定义错误处理程序抛出405的asmx WebService
本文关键字:asmx WebService 错误 处理 程序 自定义 | 更新日期: 2023-09-27 18:07:36
我已经设置了一个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上的声明。配置如下:
<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的回答响应)。TrySkipIisCustomErrors不工作).
我的理解是,你有两种类型的错误响应;Replace
和PassThrough
。PassThrough
完全返回. net生成的内容(跳过IIS错误),Replace
用您在该代码中定义的任何内容替换响应。当我将此设置为Replace
时,它正在废弃我的WebService响应并将其替换为自定义响应,由于runAllManagedModulesForAllRequests
标志被设置为true,试图重新运行WebService调用,但没有请求体-因此原始错误405作为param1丢失。
希望这能帮助别人节省一天的调查时间!