ASP.NET ToolkitScriptManager AsyncPostBackError - throw exception from Content Page - asp.net

Wonder if anyone can help. I'm trying to implement the ToolScriptManager OnAsyncPostBackError.
The ToolkitScriptManager is on the Masterpage and I've set the OnAsyncPostBackError property:
<ajax:ToolkitScriptManager ID="ToolkitScriptManager"
runat="server" EnablePartialRendering="true"
OnAsyncPostBackError="ScriptManager_AsyncPostBackError">
</ajax:ToolkitScriptManager>
On my content page I'm catching an exception and, logging it and then throwing it
private void LogError(Exception ex, bool full)
{
...
_presenter.LogError(this, error, ex);
throw new ApplicationException(
"Error Occured",
ex
);
}
where it then gets picked up by the handler on the Master Page
protected void ScriptManager_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
if (e.Exception.Data["GUID"] != null)
{
string _message =
(
e.Exception.InnerException != null
) ? e.Exception.InnerException.Message : e.Exception.Message;
ToolkitScriptManager.AsyncPostBackErrorMessage = _message +
" When reporting this error use the following ID: " +
e.Exception.Data["GUID"].ToString();
}
else
{
ToolkitScriptManager.AsyncPostBackErrorMessage =
"The server could not process the request.";
}
}
This all works as expected, I'm picking up the error via the PageRequestManager EndRequest and displaying on the page
The problem is that I'm getting an Application_Error in my Global.asax stating that an Exception of type 'System.Web.HttpUnhandledException' was thrown.
I kinda get it I think, cause I'm throwing the exception which is not caught by a catch I'm assuming?!
Any advice on what I'm missing, or where in fact I'm going wrong?
Thanks in advance!
Ian

Related

How to Uncache current page from outputcache in ASP.Net

I have enabled page caching for my ASPX pages with
<%# OutputCache Duration="7200" VaryByParam="*" Location="Server" %>
However, the next time the page is regenerated, if there happens to be an error in the page, that also gets cached, and the site continues to display the page with errors in it for the next 7200 seconds or until some dependancy flushes the cache.
Currently I tried adding the sites error log as a file dependancy, so that anytime an error is logged, pages are refreshed. However, that causes the pages to be refreshed, even when another page in the site has an error.
Question is, How can I put a piece of code in the error handling block to uncache the current page..
Pseudo code.
try
{
page load
}
catch (Exception ex)
{
// Add C# code to not cache the current page this time.
}
You can simply use HttpResponse.RemoveOutputCacheItem as follows:
try
{
//Page load
}
catch (Exception ex)
{
HttpResponse.RemoveOutputCacheItem("/mypage.aspx");
}
See: Any way to clear/flush/remove OutputCache?
Another way to do that catch exception in Application_Error and use Response.Cache.AddValidationCallback, from this solution:
public void Application_Error(Object sender, EventArgs e) {
...
Response.Cache.AddValidationCallback(
DontCacheCurrentResponse,
null);
...
}
private void DontCacheCurrentResponse(
HttpContext context,
Object data,
ref HttpValidationStatus status) {
status = HttpValidationStatus.IgnoreThisRequest;
}

Using ELMAH and Application_Error at the same time

We have an ASP.NET MVC 3 application that is configured to use ELMAH. We also have code in our Application_Error method like this. Both ELMAH and our custom code log to a database.
protected void Application_Error(object sender, EventArgs e)
{
MvcApplication app = (MvcApplication)sender;
HttpContext httpContext = app.Context;
Exception ex = app.Server.GetLastError();
HttpException httpException = ex as HttpException;
//log the error with our custom logging
Server.ClearError();
if (httpContext.IsCustomErrorEnabled) //only show custom error if enabled in config
{
httpContext.Response.Clear();
httpContext.ClearError();
//show our own custom error page here
}
}
The problem (not really a problem, but whatever) we see is that both ELMAH and our custom code log the exception to the DB. I would expect calls to Server.ClearError() and httpContext.ClearError would handle the error and it would never get to ELMAH. But, does the fact that the error is being logged twice imply that ELMAH and application_error are running basically in parallel and they both receive the unhandled exception at the same time? If so is there anyway to tell ELMAH to ignore the error?
Our intent is to only have ELMAH handle an error if something goes REALLY wrong, like in the ASP.NET pipeline after elmah is registered, but before the MVC application would be running.
The issue is that it is logging to ELMAH first. So yes you can tell it not to log to ELMAH using e.Dismiss(): The ErrorLog_Filtering function below is hit before Application_Error. So add this function and any needed logic you need to determine if you want it in ELMAH or not.
void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
//get the exceptions like:
Exception m = e.Exception;
Exception ex = e.Exception.GetBaseException();
//tell it not to log the error in ELMAH like (based on whatever criteria you have):
e.Dismiss();
//Application_Error will be hit next
}
protected void Application_Error(object sender, EventArgs e)
{
//your logic
}

asp.net + exceptions and redirecting

My intention is to log an error(I am using Log4Net) when an exception is caught and redirect to a pretty looking page with some error message. I have a class that returns a Type T object , mostly a DataSet.
In my Catch statement I wrote this, it works but I am not sure if there's a more appropriate way of handling, can someone please advice. Thanks. Note that the throw cannot be omitted because the class has a return type.:
catch (Exception ex)
{
log.Error(ex);
HttpContext.Current.Response.Redirect("~/errorPage.aspx");
throw ex;
}
It depends upon how you want to handle error on the page, In general , unhandled exception should be bubbled up to application_error in gloabl.asax file to it generic.Here is one simple way to handle this error.
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Get the exception object.
Exception exc = Server.GetLastError();
// Handle HTTP errors
if (exc.GetType() == typeof(HttpException))
{
// The Complete Error Handling Example generates
// some errors using URLs with "NoCatch" in them;
// ignore these here to simulate what would happen
// if a global.asax handler were not implemented.
if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
return;
//Redirect HTTP errors to HttpError page
Server.Transfer("HttpErrorPage.aspx");
}
// For other kinds of errors give the user some information
// but stay on the default page
Response.Write("<h2>Global Page Error</h2>\n");
Response.Write(
"<p>" + exc.Message + "</p>\n");
Response.Write("Return to the <a href='Default.aspx'>" +
"Default Page</a>\n");
// Log the exception and notify system operators
ExceptionUtility.LogException(exc, "DefaultPage");
ExceptionUtility.NotifySystemOps(exc);
// Clear the error from the server
Server.ClearError();
}

Display exception message using JavaScript in Base Page's OnError event

I want to display all unhandled excpetions in the appplication using Javascript. For this I have defined onError event inside my custom base class of my pages. Here is the code for my Base Page:
namespace Loan
{
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
}
protected override void OnError(EventArgs e)
{
//Report Error
Exception ex = Server.GetLastError();
if (ex is HttpUnhandledException && ex.InnerException != null)
{
ex = ex.InnerException;
}
var _message = "Error : "+ ex.Message.ToString();
DisplayAlert(_message);
Server.ClearError();
return;
}
protected virtual void DisplayAlert(string message)
{
ClientScript.RegisterStartupScript(
this.GetType(),
Guid.NewGuid().ToString(),
string.Format("alert('{0}');", message.Replace("'", #"\'")),
true
);
}
}
}
The alert is never displayed for an unhandled exception. However, if I call the DisplayAlert from any Page
base.DisplayAlert(ex.Message);
the javascript alert is displayed. How can I get the javascript alert to be displayed for the unhandled exceptions from the base page.Or is there any other way to display these exception messages to the user. I don't want to redirect them to a generic error page as it sends them back and forth.
This is expected. If the exception is unhandled, the OnError event on BasePage will execute and your child page won't continue to execute, there's nothing to render as the BasePage is pure code. If you want to spit out the alert you'd need to write directly to the Response but you should still see a blank page after an unhandled exception occurs.
protected virtual void DisplayAlert(string message)
{
Response.Write(string.Format("<script>alert('{0}');</script>", message.Replace("'", #"\'")));
}
Of course, when you call DisplayAlert directly, it works because you are just calling a method, the Page execution continues normally.
I frankly dislike your approach. You should log the exception and redirect to another page, the typical Oooooooooopsss, me screwed up kind of thing.

Getting Illegal characters in path exception after enabling HttpUnhandledException

I added to my .net app in the Global.asax.cs file error trapping like so
void Application_Error(object sender, EventArgs e)
{
HttpUnhandledException httpUnhandledException = new
HttpUnhandledException(Server.GetLastError().Message, Server.GetLastError());
SystemFunctions.SendEmailWithErrors(httpUnhandledException.GetHtmlErrorMessage());
}
When I run my app I get an Illegal Characters in Path exception in the Designer.Cs funciton below..
public OptionsEntities() : base("name=OptionsEntities", "OptionsEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
If I just continue on the app keeps going. If I remove the code block from the Global.asax.cs, the error goes away. Any ideas on where this may be coming from??
try to relpace "name=OptionsEntities",with "OptionsEntities" may be it help you

Resources