c# - 什么处理对 asp.net 页面中的 WebMethod 的请求

标签 c# asp.net web-applications exception webmethod

我正在尝试实现通用错误处理。

public partial class SomePage : System.Web.UI.Page
{
//...
    [WebMethod()]
    public static int SomeMethod()
    {
        //... some code
        //... exception
    }
//...
}

可以使用 global.asax 处理常规请求,但静态 WebMethods 不会通过 Application_BeginRequest 或 Application_Error。我尝试编写一个属性,从 ExceptionFilterAttribute 派生并使用 OnException 覆盖,但它也不起作用。

那么谁来处理这些请求?

客户端调用是通过下面的 jQuery ajax 请求进行的:

$.ajax({
    type: "POST",
    url: "SomePage.aspx/SomeMethod",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        someVar = response.d;
    },
    error: function (a, b, c) {
        console.log(a); 
        // handle 
    } 
});

最佳答案

从文档来看,您似乎需要构建一个 SOAP 扩展来全局处理错误 - 下面是有用的片段和链接:

A Web application can be comprised of multiple Web services. However, the Application_Error event within the Global.asax Syntax file cannot be used for global exception handling. The HttpHandler for Web services consumes any exception that occurs while a Web service is executing and turns it into a SOAP fault before the Application_Error event is called. Build a SOAP extension to process Web service exceptions in a global exception handler. A SOAP extension can check for the existence of an exception in the ProcessMessage method. Within the ProcessMessage method, check the Exception property of the SoapMessage passed when the Stage property is set to AfterSerialize. For details on SOAP extensions, see SOAP Message Modification Using SOAP Extensions.

摘自 Handling and Throwing Exceptions in XML Web Services (我相信也有一些例子可以帮助你一路走来)

此外,Rick Strahl (MVP) 不久前就该主题写了这篇文章,它就地解决了问题,但没有解决您在全局范围内管理问题的需要 http://www.west-wind.com/weblog/posts/2005/Jan/29/NET-Web-Services-and-unhandled-Exceptions

这篇文章将指导您根据您的要求构建用于全局错误处理的 soap 扩展 http://geekswithblogs.net/pavelka/archive/2005/09/05/HowToCreateAGlobalExceptionHandlerForAWebService.aspx

关于c# - 什么处理对 asp.net 页面中的 WebMethod 的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129917/

相关文章:

c# - 仅当项目中存在属性时才在 aspx 中使用 Eval - 抛出 DataBinding 异常

c# - 从数据库查询返回一些值

c# - Entity Framework 尝试在对象为空时再次延迟加载对象

c# - LINQ 按自定义类型分组不起作用

c# - 为什么我无法在 Visual Studio 2010 中找到或使用 UrlEncode?

tomcat - Spring MVC 3.1, Controller 不工作

jsp中web.xml中的Java web应用程序全局参数

c# - 如何在不同的桌面浏览器上加载不同的 View

c# - MenuItem 的 ClientID

php - 关于为每个客户存储照片以便他们可以访问的解决方案的建议