Asp.net Webmethod Return Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site asp.net mvc controller return 403 About Us Learn more about Stack Overflow the company Business Learn more about
C# Throw 403 Exception
hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss asp.net mvc throw 403 Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to return error
Web Api Return Forbidden
from webmethod? up vote 3 down vote favorite suppose i am going to call aspx page method decorated with webmethod. so i need to know that how to return error from my webmethod as a result jquery error portion will be invoked and show the error detail? below is a sample code by which i am calling my aspx method. $.ajax({ type: "POST", url: "./Default.aspx/GetData", http status 403 forbidden error when trying to access webservice data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: AjaxSucceeded, error: AjaxFailed }); [WebMethod] public static string GetData() { } so please help in terms of code that how to return error from my webmethod as a result jquery error portion will be invoked and show the error detail...guide me in detail. thanks jquery asp.net share|improve this question edited Aug 27 '11 at 17:41 Yuck 29.5k867107 asked Aug 27 '11 at 17:37 Thomas 11.2k70247437 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted I don't know if there's a more WebMethod-specific way of doing it, but in ASP.NET you'd generally just set the status code for your Response object. Something like this: Response.Clear(); Response.StatusCode = 500; // or whatever code is appropriate Response.End; Using standard error codes is the appropriate way to notify a consuming HTTP client of an error. Before ending the response you can also Response.Write() any messages you want to send. The formats for those are much less standardized, so you can create your own. But as long as the status code accurately reflects the response then your JavaScript or any other client consuming that
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings
Actionresult 403
and policies of this site About Us Learn more about Stack Overflow
C# Httpexception
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation c# httpstatuscode Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; http://stackoverflow.com/questions/7216195/how-to-return-error-from-webmethod it only takes a minute: Sign up Catching errors from calling ASP.NET WebMethod with malformed Json up vote 12 down vote favorite 1 We have an older ASP.NET WebForms application which performs AJAX request by using jQuery $.ajax() calls on the client side, calling static methods in the page code-behind decorated with [WebMethod] attributes. If an unhandled exception occurs within http://stackoverflow.com/questions/24054573/catching-errors-from-calling-asp-net-webmethod-with-malformed-json the WebMethod, it does not fire the Application_Error event and is thus not picked up by our error logger (ELMAH). This is well known and not a problem - we have all WebMethod code wrapped in try-catch blocks with exceptions being manually logged to ELMAH. However, there is one case that has me stumped. If malformed Json is posted to the WebMethod URL, it throws an exception before entering our code, and I can't find any way to trap this. e.g. this WebMethod signature [WebMethod] public static string LeWebMethod(string stringParam, int intParam) Normally called with a Json payload like: {"stringParam":"oh hai","intParam":37} I tried a test using Fiddler to edit the payload to the malformed Json: {"stringParam":"oh hai","intPara And got the following ArgumentException error response from JavaScriptObjectDeserializer sent to the client (this is in a simple test app running locally with no custom errors): {"Message":"Unterminated string passed in. (32): {\"stringParam\":\"oh hai\",\"intPara","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeString()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeMemberName()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.Ja
April 30, 2012 Apr 302012 Share...0000000 Here is the problem: How to properly display a user-friendly error message for an exception being thrown from an ASP.NET http://www.agile-code.com/blog/asp-net-web-service-returning-properly-formatted-error-message/ Web Service? I know that we shouldn't be really working anymore with ASP.NET Web Services as a huge amount of problems have been solved with the WCF framework. But, for those like me, that are working on some large-scale projects where we are still using some old technology this could be an interesting post. I was asp.net mvc kind of surprised the first time I saw an exception being thrown from the ASP.NET Web Service and how it looked ugly when the error Message Box appeared on the Client application (GUI). The Exception.Message contained the internal Web Service stack. The server-side code looked like this: [WebMethod] public SomeResultObject SomeWebMethod() { try { /* code here asp.net webmethod return */ } catch (Exception ex) { /* Some logging logic here...*/ throw; //this would simply throw an SoapException } } The first thing I tried was to throw my Exception, and not just re-throwing, but this was not helping at all as I soon realized that all the exceptions thrown by the SomeWebMethod() would be wrapped internally by the .NET framework, and the SoapException would be thrown instead. The first approach - Client side My first approach of solving this problem was then to change the Client (GUI) application and display a generic error message, no matter what has been thrown by the Web Service. Obviously this was a bad choice, as there were so many conditions and error messages that the end-user needed to know about. So, this approach would probably work for someone, but unfortunately didn't work for me: public void WebServiceCallMethod() { try { /* Calling the web service here */ } catch(SoapException exc) { MessageBox.Show("An error happened while processing your call"); } } Parsing