从客户端的 ASP.NET PageMethods 获取异常详细信息

发布于 2024-07-22 17:46:45 字数 729 浏览 5 评论 0原文

我有一个 pagemethod,我可以从 JavaScript 调用它 说

Pagemethods.MyMethod(MyParameter, onsucess, onfailure);

在后面的代码中,我有这样的内容:

[WebMethod]
public static void MyMethod(Param)
{
   try{
     //DoSomething..
   }
   catch(exception ex)
   {
      //Log the exception and rethrow
      throw new exception(ex.innerexception);
   }
}

现在我面临的问题是:

每当我确实遇到异常时, 我从后面的代码中重新抛出异常

但是在 onfailure 方法中,我只是收到一条通用消息说 “服务器方法 MyMethod 因以下错误而失败:”

我似乎没有获取异常详细信息,而只获取通用异常,

我如何获取 JavaScript 上的异常详细信息,以便根据 UI/JavaScript 端处理它。

我已经验证,这不是 web.config 中自定义错误设置的问题。

有人可以告诉我这里发生了什么吗?

PS:我已经单步执行了每一行代码,并且记录后的异常会使用正确的异常详细信息(即消息)重新抛出。

I am having a pagemethod to which i give a call from my JavaScript
say

Pagemethods.MyMethod(MyParameter, onsucess, onfailure);

In the Code behind, I have something like this:

[WebMethod]
public static void MyMethod(Param)
{
   try{
     //DoSomething..
   }
   catch(exception ex)
   {
      //Log the exception and rethrow
      throw new exception(ex.innerexception);
   }
}

Now the issue i face is :

Whenever i do get an exception,
i re throw the exception from code behind

But in the onfailure method, i just get a generic message saying that
"the server method MyMethod failed with following error: "

I don't seem to get the exception details and only that generic exception,

How can i get the exception details on JavaScript, in order to handle it according on the UI/JavaScript side.

I have verified, that it is not an issue with custom errors settings in web.config.

Can some one enlighten to me as to what is happening here?

PS: i have stepped through each and every line of code and the exception after being logged is rethrown with the proper exception details i.e. message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_失温 2024-07-29 17:46:45

据我了解,只要您

<customErrors mode="off" />

的 web.config 中包含该消息,该消息就会返回给客户端。 你确定有这个设置吗?

要显示与错误相关的消息,您需要将函数名称作为页面方法调用的第三个参数:该函数可以简单如下:

function onfailure( result )
{
   alert( result.get_message() );
}

这就是我们所拥有的,并且工作正常

As far as I understand as long as you have

<customErrors mode="off" />

in your web.config, the message will be returned to client. Are you sure you have this setting ?

To display the message associated with error you need to have oassed the name of the function as the third parameter of the page method call : this function could be as simple as:

function onfailure( result )
{
   alert( result.get_message() );
}

That's what we have and it works OK

呆° 2024-07-29 17:46:45

在 web.config 的 部分设置 后,尝试这些函数来获取所有PageMethod 的 onfailure 的异常详细信息:

function onfailure(error) {                
    alert("Error: " + error.get_message() + "; " + 
        "Stack Trace: " + error.get_stackTrace() + "; " + 
        "Status Code: " + error.get_statusCode() + "; " + 
        "Exception Type: " + error.get_exceptionType() + "; " + 
        "Timed Out: " + error.get_timedOut());
}

我发现错误的状态代码比错误消息本身更有帮助。

After you have set <customErrors mode="off" /> in the <system.web> section of the web.config, try these functions to get all of the exception details from the PageMethod's onfailure:

function onfailure(error) {                
    alert("Error: " + error.get_message() + "; " + 
        "Stack Trace: " + error.get_stackTrace() + "; " + 
        "Status Code: " + error.get_statusCode() + "; " + 
        "Exception Type: " + error.get_exceptionType() + "; " + 
        "Timed Out: " + error.get_timedOut());
}

I've found the error's status code to be far more helpful than the error message itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文