Asp.net Ashx Error Handling
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
Asp.net Error Handling
Learn more about Stack Overflow the company Business Learn more about hiring developers asp.net custom error page or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack exception handling in asp.net c# 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 Error handling when downloading file from ASP.NET
Asp.net Mvc Custom Error Page
Web Handler (.ashx) up vote 3 down vote favorite 2 I have a web page which a user can download a PDF file via an ASP.NET web handler (.ashx). It is implemented like the answer in this question. The problem I have is when I do do this window.top.location.href = url; in my JavaScript I have no real control over what happens if there is an exception
Asp.net Application_error
thrown in the handler. The user experience, when everything works correctly, is that they essentially stay on the page they were on and the browser tells them that they can download the PDF file. However, when there is an exception thrown in the handler they are redirected to the URL of the handler and shown a blank page. Here is some example code to make it more clear: JavaScript: function openPDF() { var url = GeneratePDFUrl(); window.top.location.href = url; } Handler: public override void Process(HttpContext context) { byte[] pdfdata = GetPDFData(); context.Response.ContentType = "application/pdf"; context.Response.AddHeader("content-disposition", "attachment; filename=\"" + GetPDFFileName() + "\""); context.Response.AddHeader("content-length", pdfdata.Length.ToString()); context.Response.BinaryWrite(pdfdata); } The problem happens when GetPDFData() throws an exception. We are doing what we can to prevent GetPDFData() from throwing an exception, but it is generated from user input so we're also handling it here in case there are cases we don't/can't predict which generate an error. Here is one solution I have come up with, but it shows the user error text (instead of a blank page) public override void Process(HttpContext context) { try { byte[] pdfdata = GetPDFData(); WritePDF(pdfdata, GetPDFFileName()); // Executes code in example above } catch (Exception e) { context.Response.Clear(); context.Response.Write
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 Business Learn more about hiring developers asp.net error handling best practices or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Asp.net Error Logging
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it asp.net mvc error handling only takes a minute: Sign up Catch exceptions in ASP.NET resulting from Generic Handler (ashx) file up vote 1 down vote favorite 1 I am trying to get a different server side stack functioning with my ASP.NET site. My http://stackoverflow.com/questions/14609389/error-handling-when-downloading-file-from-asp-net-web-handler-ashx ashx file seems to be throwing a 500 Internal Server Error. How do I figure out what exception this ashx file is throwing or the reason the 500 is being thrown? When I attach to my IIS 7 w3wp.exe process, it doesn't throw an exception, but I'm reading that this is likely the case. The ashx file lives in the root directory of my ASP.NET site, and the Properties have it set to compile. GET http://uhc-8:8883/MyApp/ExtDirectProxy.ashx 500 (Internal Server Error) http://stackoverflow.com/questions/26135251/catch-exceptions-in-asp-net-resulting-from-generic-handler-ashx-file index.html:27 Uncaught TypeError: Cannot read property 'isProvider' of null ext-all-debug.js:74832 allow right click initializing for http://uhc-8:8883/MyApp/index.html rightclick.js:1 allow right click processing http://uhc-8:8883/MyApp/index.html rightclick.js:25 c# typeerror ashx app-code ext-direct share|improve this question edited Dec 5 '15 at 7:09 asked Oct 1 '14 at 6:20 MacGyver 8,11018100186 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote You can use the Application_Error event in Global.asax file to catch that error. protected void Application_Error(Object sender, EventArgs e) { HttpContext ctx = HttpContext.Current; Exception exception = ctx.Server.GetLastError(); int httpCode = ((HttpException)exception).GetHttpCode(); // check if it is 500 } share|improve this answer answered Oct 1 '14 at 6:24 Amit Joki 39.6k63163 Looks like I had that file, and had the Application_Error method, but I didn't have it implemented (no code in there). I added your code, and put a breakpoint, but it didn't hit. –MacGyver Oct 1 '14 at 6:27 any other ideas? –MacGyver Oct 1 '14 at 6:33 @MacGyver it is the root handler. Have you tried the error handler in ashx? –Amit Joki Oct 1 '14 at 6:37 How do I make it the root handler? No, I have not tried, or heard of that. –MacGyver Oct 1 '14 at 6:49 1 @MacGyver meant to say Application_Error is the root handler.. –Amit Joki Oct 1 '14 at 6:50 add a comment| up vote 1 down vote accepted
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples https://msdn.microsoft.com/en-us/library/bb397417.aspx We’re sorry. The content you requested has been removed. You’ll be auto redirected http://forums.asp.net/t/1561919.aspx?Problem+with+ashx+page in 1 second. MSDN Library MSDN Library MSDN Library MSDN Library Design Tools Development Tools and Languages Mobile and Embedded Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies Web Development Windows Desktop App Development TOC Collapse the table of content Expand the table of error handling content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Complete Example for Error Handlers Other Versions Visual Studio 2010 .NET Framework 4 Visual Studio 2008 This code example includes elements for both page-level and application-level exception handling. Code Example Files The example consists of the following files: Web.config Global.asax Default.aspx ExceptionUtility (to be put in the App_Code asp.net error handling folder) GenericErrorPage.aspx HttpErrorPage.aspx Http404ErrorPage.aspx DefaultRedirectErrorPage.aspx Web.config The following example shows the Web.config file. The customErrors section specifies how to handle errors that occur with file types that are mapped to ASP.NET, such as .aspx, .asmx, and .ashx files. (In IIS 6.0 and in IIS 7.0 in classic mode, static content files such as .html and .jpg files are not mapped to ASP.NET.) The settings in the example customErrors section cause any unhandled HTTP 404 (file not found) errors to be directed to the Http404ErrorPage.aspx file. These HTTP 404 errors would occur if a request were made for an .aspx file, .asmx file, and so on and if the requested file did not exist. All other unhandled errors in ASP.NET files are directed to the DefaultRedirectErrorPage.aspx file. If static content files are not handled by ASP.NET, a request for a nonexistent .html or .jpg file does not cause a redirect to the Http404ErrorPage.aspx file. If you want ASP.NET to handle requests for all file types, you can configure IIS to map file-name extensions to ASP.NET. Note In the example, the mode attribute is set to "On" so that you can error messages when you run the example in Visual Studio