Asp.net Webmethod Return Http 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 About Us Learn more about Stack Overflow the company asp.net mvc controller return 403 Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs c# throw 403 exception Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, asp.net mvc throw 403 just like you, helping each other. Join them; it only takes a minute: Sign up How to return error from webmethod? up vote 3 down vote favorite suppose i am going to call aspx page method decorated web api return forbidden 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", 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
Http Status 403 Forbidden Error When Trying To Access Webservice
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 service will understand the error. share|improve this answer answered Aug 27 '11 at 17:51 David 111k18106179 add a comment| up vote 3 down vote Just throw the exception in your PageMethod and catch it in AjaxFailed. Something like that: function onAjaxFailed(error){ alert(error); } share|improve this answer answered Aug 27 '11 at 17:58 phnkha 5,2031
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the
Actionresult 403
workings and policies of this site About Us Learn more about Stack c# httpexception Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions c# httpstatuscode Jobs Documentation 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 http://stackoverflow.com/questions/7216195/how-to-return-error-from-webmethod them; it only takes a minute: Sign up Can I Set the HTTP Response Code & Throw an Exception on an ASMX JSON Service? up vote 4 down vote favorite In an ASP.NET ASMX WebMethod that responds JSON, can i both throw an exception & set the HTTP response code? I thought if i threw an HttpException, the status http://stackoverflow.com/questions/14262344/can-i-set-the-http-response-code-throw-an-exception-on-an-asmx-json-service code would be set appropriately, but it cannot get the service to respond with anything but a 500 error. I have tried the following: [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public void TestWebMethod() { throw new HttpException((int)HttpStatusCode.BadRequest, "Error Message"); } Also: [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public void TestWebMethod() { try { throw new HttpException((int)HttpStatusCode.BadRequest, "Error Message"); } catch ( HttpException ex ) { Context.Response.StatusCode = ex.GetHttpCode(); throw ex; } } These both respond with 500. Many thanks. asp.net error-handling asmx http-status-codes webmethod share|improve this question asked Jan 10 '13 at 16:16 Markus 126114 Did you get anywhere with this? –Colin Goudie Feb 11 '13 at 1:27 I am afraid not. –Markus Feb 11 '13 at 15:36 add a comment| 1 Answer 1 active oldest votes up vote 2 down vote Change your code to this: [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public void TestWebMethod() { try { throw new HttpException((int)HttpStatusCode.BadRequest, "Error Message"); } catch ( HttpException ex ) { Context.Response.StatusCode = ex.GetHttpCode(); // See Markus
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 Web Service? I know that we shouldn't be really working anymore with ASP.NET Web Services http://www.agile-code.com/blog/asp-net-web-service-returning-properly-formatted-error-message/ 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 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 asp.net mvc 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 */ } 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 asp.net webmethod return 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 the exc.Message in order to retrieve the error message was possible but was avoided and taken as a last resort. For sure this is not a correct approach as that is not really maintainable. Fixing the Web Service - Return parameter The second obvious solution was to change the Web Service, and the solution lies in the Rerturn object. I've soon realized that I could change my return parameter to include the ErrorMessage and to return that to the Client Application, as follows: public class SomeResultObject { public string ErrorMessage {