Why doesn't Controller Factory use a Controller returned by that factory? - asp.net

I implemented a custom controller factory in ASP.NET MVC, and I registered it in global.ascx. The idea is to handle the case of 404 and also exceptions in the controller constructors. I know the factory has been assigned to ASP.NET MVC, because on requests, I can step into it. I can see that I'm returning the controller that I think. But why, oh why on earth, is not my controller used? But I'd think I'd get the usual action not found exception, not controller..conceptually I'm wondering if this is even the right spot to do this in.
protected override IController GetControllerInstance
(RequestContext context,
Type controllerType)
{
IController controller = null;
try
{
controller = base.GetControllerInstance(context, controllerType);
}
catch (CurrentSessionException)
{
controller = new LoginController();
}
catch (System.Web.HttpException)
{
controller = new ErrorController();
}
catch (System.Exception)
{
controller = new ErrorController();
}
return controller;
}

Try manually clearing the errors in your catch statement.
requestContext.HttpContext.ClearError();
Ideally this is best handled as a Filter. MVC comes with a HandleErrorAttribute which you can subclass. You would override the OnException method and then simple handle the logic as you wish.
This is what MVC 3 does by default.
public virtual void OnException(ExceptionContext filterContext) {
if (filterContext == null) {
throw new ArgumentNullException("filterContext");
}
if (filterContext.IsChildAction) {
return;
}
// If custom errors are disabled, we need to let the normal ASP.NET exception handler
// execute so that the user can see useful debugging information.
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) {
return;
}
Exception exception = filterContext.Exception;
// If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
// ignore it.
if (new HttpException(null, exception).GetHttpCode() != 500) {
return;
}
if (!ExceptionType.IsInstanceOfType(exception)) {
return;
}
string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult {
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
// Certain versions of IIS will sometimes use their own error page when
// they detect a server error. Setting this property indicates that we
// want it to try to render ASP.NET MVC's error page instead.
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}

Related

ASP.NET MVC Exception Handling with AJAX/JSON

I have several methods in a controller that look like:
[HttpPost]
public ActionResult AddEditCommentToInvoice(string invoiceNumber, string comments)
{
var response = new { success = true, msg = "Comment saved", statusMsg = "Comment saved" };
try
{
var recordsModified = invoiceService.AddCommentsToInvoice(invoiceNumber, comments);
Log.Info(recordsModified ? "Updated Comment" : "Did not update Comment");
} catch (Exception ex) {
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return Json(new {
success = false,
msg = "There is missing field data",
statusMsg = ex.Message
}, JsonRequestBehavior.AllowGet);
}
return Json(response, JsonRequestBehavior.AllowGet);
}
While this code works, I'm not comfortable with this approach because:
Try/Catches are expensive
The code catches System.Exception
The code is ugly
Now I know that I can use OnException or the HandleError attribute.
I also did some research on ELMAH and this looks promising.
But I still want to return JSON via AJAX to my user to indicate whether the operation was a success or not.
So my question is, has anyone used any of the three methods (or specifically ELMAH) to return JSON via AJAX?
I use another approach that's an approach that can be applied at the controller level or globally through GlobalFilters. In my MVC controllers, you could override OnActionExecuted method, and do this:
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Exception != null)
{
filterContext.Result = Json(new { success = false });
return;
}
base.OnActionExecuted(filterContext);
}
This could also be done as an action filter attribute. You wouldn't need any exception handling in your controllers - if an exception occurs, then this is handled within the context of the result.

ASP.NET MVC 4 Custom Error Views issue

I'm working on creating custom error pages in my ASP.NET MVC 4 web application. I've already implemented OnException error handling and controller decorating with attributes, but I wanted to have custom error pages for URLs that didn't fall under any controller routes so I resorted to the Global.asax's Application_Error.
Following #Darin's (thanks btw!) response in this question here, I have everything setup accordingly but I get an issue when the controller renders the view on the client side. When it returns a view, it returns all of the HTML rendered inside of a<pre> tag. I'm using Chrome's dev tools when I see this. This happens in Chrome, Safari, and Mozilla (maybe more, I didn't bother to check after those). Oddly, the views render fine in IE though. I've googled around and looked around other questions and haven't found much on this. Would anyone happen to know how/why this happens, and a workaround fix for it? As a last ditch effort, I thought about getting the View as a string and using return Content(...) instead, but it seems a little much for what it's worth.
tl;dr - If I could have some input on why my view HTML is being rendered inside of a <pre> tag, that would be awesome! I'm sure it's browser-rendering specific, but I'm not going to settle on this only works in IE.
Controller:
public class ErrorsController : Controller
{
public ActionResult PageNotFound()
{
Response.StatusCode = 404;
var vm = new ErrorViewModel()
{
ErrorMessage = "The page you requested could not be found.",
StatusCode = Response.StatusCode
};
Response.TrySkipIisCustomErrors = true;
return View("PageNotFound", vm);
}
View:
#{
Layout = "~/Views/Shared/_GenericLayout.cshtml";
}
<h1 class="error">Oh No!</h1>
#if(Model.ErrorMessage != null && Model.StatusCode != null)
{
<h2><em>#Model.StatusCode</em> - #Model.ErrorMessage</h2>
}
else
{
<h2>The page you requested could not be found.</h2>
}
Global.asax:
protected void Application_Error(object sender, EventArgs e)
{
if (Context.IsCustomErrorEnabled)
{
ShowCustomErrorPage(Server.GetLastError());
}
}
private void ShowCustomErrorPage(Exception exception)
{
HttpException httpException = exception as HttpException;
Response.Clear();
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Errors");
routeData.Values.Add("fromAppErrorEvent", true);
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch(Response.StatusCode)
{
case 404:
routeData.Values.Add("action", "PageNotFound");
break;
case 403:
routeData.Values.Add("action", "PageForbidden");
break;
}
}
Server.ClearError();
IController controller = new ErrorsController();
RequestContext rc = new RequestContext(new HttpContextWrapper(Context), routeData);
controller.Execute(rc);
}
Simple fix. Chrome wasn't recognizing what it was receiving, so explicitly defining the response's content type to "text/html" before returning the view fixed the problem.
public ActionResult PageNotFound()
{
Response.StatusCode = 404;
var vm = new ErrorViewModel()
{
ErrorMessage = "The page you requested could not be found.",
StatusCode = Response.StatusCode
};
Response.ContentType = "text/html";
Response.TrySkipIisCustomErrors = true;
return View("PageNotFound", vm);
}

ASP.NET Web API Exception filter - A way to get request url from HttpRequestBase?

I've implemented a custom exception filter to my Web API. It is working as intended, except for one small detail...
In the following code sample, SaveToErrorLog saves exception details and tries to get the request url from context.Request.RawUrl. But context.Request does not contain the url that the API tried to serve when the exception happened. Is there a way to get the url when using an exception filter like this?
public class APIExceptionFilter : ExceptionFilterAttribute
{
private HttpContextBase context;
public APIExceptionFilter()
{
context = new HttpContextWrapper(HttpContext.Current);
}
public override void OnException(HttpActionExecutedContext actionContext)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
if (actionContext != null && context != null)
{
facade.SaveToErrorLog(actionContext.Exception, context.Request);
}
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(actionContext.Exception.Message),
ReasonPhrase = "APIException"
});
}
}
As per the comment above by #emre_nevayeshirazi, you need to use the HttpActionExecutedContext. This gives you access to the request and then the required Uri.
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
var requestedUri = actionExecutedContext.Request.RequestUri;
//Do something
}

Redirect from model in ASP.NET mvc3

Apologies if this is a silly question, but I'm just wondering the best way to go about redirecting to an error page when an error occurs in a model in asp.net mvc3.
In short, I have an applicationController that all controllers inherit, which, in the "OnActionExecuting" function, checks to see if the user is using Internet Explorer.
If they are, I call a function in my error model (which I have because I want to log the error in the database), which should then redirect the user to an error page telling to download chrome.
ApplicationController
public class ApplicationController : Controller
{
public string BASE_URL { get; set; }
public ApplicationController() {}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
BASE_URL = Url.Content("~/");
ErrorModel err = new ErrorModel();
String userAgent;
userAgent = Request.UserAgent;
if (userAgent.IndexOf("MSIE") > -1) {
err.cause("browser", "redirect", "some other info");
}
}
}
ErrorModel
public void cause(string _errCode, string strAction, string _otherInfo = "") {
this.error_code = _errCode;
this.error_otherInfo = _otherInfo;
try {
this.error_message = dctErrors[_errCode.ToLower()];
} catch (KeyNotFoundException) {
this.error_message = "Message not found.";
}
StackTrace sT = new StackTrace(true);
String[] filePathParts = sT.GetFrame(1).GetFileName().Split('\\');
this.error_filename = filePathParts.Last();
this.error_lineNo = sT.GetFrame(1).GetFileLineNumber();
this.error_function = sT.GetFrame(1).GetMethod().ReflectedType.FullName;
this.error_date = DateTime.Now;
this.error_uid = 0; //temporary
if (strAction == "redirect") {
//this is what I would like to do - but the "response" object does not
//exist in the context of the model
Response.Redirect("Error/" + _errCode);
} else if (strAction == "inpage") {
} else {
//do nothing
}
}
I know in this particular example, the error is not actually occurring in the model, so I could just redirect from the controller. But I'd like to be able to call one function which will log and then redirect if possible, as this will be necessary for the many other errors which will likely occur.
I may be going about this entirely the wrong way, in which case I'm of course open to changing. Thanks for the help!
You can get access to the response from the HttpContext.
HttpContext.Current.Response
I personally use the global exception filter, provided by the MVC framework, to write to the error log. I also use a default redirect to an error view via the web config:
<customErrors mode="RemoteOnly" defaultRedirect="~/Error/">
There are, of course, possible drawbacks to this model but it covered most of the exceptions I've encountered so far.
ITS MUCH SIMPLE,
You need to assign the result (ActionResult) TO filterContext.Result
filterContext.Result = View("ErrorView", errorModel);
or to redirect
filterContext.Result = Redirect(url);
or
filterContext.Result = RedirectToAction("actionname");

App-wide error handling for ASP.NET MVC2 web app? [duplicate]

I have some basic code to determine errors in my MVC application. Currently in my project I have a controller called Error with action methods HTTPError404(), HTTPError500(), and General(). They all accept a string parameter error. Using or modifying the code below.
What is the best/proper way to pass the data to the Error controller for processing? I would like to have a solution as robust as possible.
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
switch (httpException.GetHttpCode())
{
case 404:
// page not found
routeData.Values.Add("action", "HttpError404");
break;
case 500:
// server error
routeData.Values.Add("action", "HttpError500");
break;
default:
routeData.Values.Add("action", "General");
break;
}
routeData.Values.Add("error", exception);
// clear error on server
Server.ClearError();
// at this point how to properly pass route data to error controller?
}
}
Instead of creating a new route for that, you could just redirect to your controller/action and pass the information via querystring. For instance:
protected void Application_Error(object sender, EventArgs e) {
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
if (httpException != null) {
string action;
switch (httpException.GetHttpCode()) {
case 404:
// page not found
action = "HttpError404";
break;
case 500:
// server error
action = "HttpError500";
break;
default:
action = "General";
break;
}
// clear error on server
Server.ClearError();
Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message));
}
Then your controller will receive whatever you want:
// GET: /Error/HttpError404
public ActionResult HttpError404(string message) {
return View("SomeView", message);
}
There are some tradeoffs with your approach. Be very very careful with looping in this kind of error handling. Other thing is that since you are going through the asp.net pipeline to handle a 404, you will create a session object for all those hits. This can be an issue (performance) for heavily used systems.
To answer the initial question "how to properly pass routedata to error controller?":
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
Then in your ErrorController class, implement a function like this:
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Error(Exception exception)
{
return View("Error", exception);
}
This pushes the exception into the View. The view page should be declared as follows:
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<System.Exception>" %>
And the code to display the error:
<% if(Model != null) { %> <p><b>Detailed error:</b><br /> <span class="error"><%= Helpers.General.GetErrorMessage((Exception)Model, false) %></span></p> <% } %>
Here is the function that gathers the all exception messages from the exception tree:
public static string GetErrorMessage(Exception ex, bool includeStackTrace)
{
StringBuilder msg = new StringBuilder();
BuildErrorMessage(ex, ref msg);
if (includeStackTrace)
{
msg.Append("\n");
msg.Append(ex.StackTrace);
}
return msg.ToString();
}
private static void BuildErrorMessage(Exception ex, ref StringBuilder msg)
{
if (ex != null)
{
msg.Append(ex.Message);
msg.Append("\n");
if (ex.InnerException != null)
{
BuildErrorMessage(ex.InnerException, ref msg);
}
}
}
I found a solution for ajax issue noted by Lion_cl.
global.asax:
protected void Application_Error()
{
if (HttpContext.Current.Request.IsAjaxRequest())
{
HttpContext ctx = HttpContext.Current;
ctx.Response.Clear();
RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
rc.RouteData.Values["action"] = "AjaxGlobalError";
// TODO: distinguish between 404 and other errors if needed
rc.RouteData.Values["newActionName"] = "WrongRequest";
rc.RouteData.Values["controller"] = "ErrorPages";
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(rc, "ErrorPages");
controller.Execute(rc);
ctx.Server.ClearError();
}
}
ErrorPagesController
public ActionResult AjaxGlobalError(string newActionName)
{
return new AjaxRedirectResult(Url.Action(newActionName), this.ControllerContext);
}
AjaxRedirectResult
public class AjaxRedirectResult : RedirectResult
{
public AjaxRedirectResult(string url, ControllerContext controllerContext)
: base(url)
{
ExecuteResult(controllerContext);
}
public override void ExecuteResult(ControllerContext context)
{
if (context.RequestContext.HttpContext.Request.IsAjaxRequest())
{
JavaScriptResult result = new JavaScriptResult()
{
Script = "try{history.pushState(null,null,window.location.href);}catch(err){}window.location.replace('" + UrlHelper.GenerateContentUrl(this.Url, context.HttpContext) + "');"
};
result.ExecuteResult(context);
}
else
{
base.ExecuteResult(context);
}
}
}
AjaxRequestExtension
public static class AjaxRequestExtension
{
public static bool IsAjaxRequest(this HttpRequest request)
{
return (request.Headers["X-Requested-With"] != null && request.Headers["X-Requested-With"] == "XMLHttpRequest");
}
}
I struggled with the idea of centralizing a global error handling routine in an MVC app before. I have a post on the ASP.NET forums.
It basically handles all your application errors in the global.asax without the need for an error controller, decorating with the [HandlerError] attribute, or fiddling with the customErrors node in the web.config.
Perhaps a better way of handling errors in MVC is to apply the HandleError attribute to your controller or action and update the Shared/Error.aspx file to do what you want. The Model object on that page includes an Exception property as well as ControllerName and ActionName.
This may not be the best way for MVC ( https://stackoverflow.com/a/9461386/5869805 )
Below is how you render a view in Application_Error and write it to http response. You do not need to use redirect. This will prevent a second request to server, so the link in browser's address bar will stay same. This may be good or bad, it depends on what you want.
Global.asax.cs
protected void Application_Error()
{
var exception = Server.GetLastError();
// TODO do whatever you want with exception, such as logging, set errorMessage, etc.
var errorMessage = "SOME FRIENDLY MESSAGE";
// TODO: UPDATE BELOW FOUR PARAMETERS ACCORDING TO YOUR ERROR HANDLING ACTION
var errorArea = "AREA";
var errorController = "CONTROLLER";
var errorAction = "ACTION";
var pathToViewFile = $"~/Areas/{errorArea}/Views/{errorController}/{errorAction}.cshtml"; // THIS SHOULD BE THE PATH IN FILESYSTEM RELATIVE TO WHERE YOUR CSPROJ FILE IS!
var requestControllerName = Convert.ToString(HttpContext.Current.Request.RequestContext?.RouteData?.Values["controller"]);
var requestActionName = Convert.ToString(HttpContext.Current.Request.RequestContext?.RouteData?.Values["action"]);
var controller = new BaseController(); // REPLACE THIS WITH YOUR BASE CONTROLLER CLASS
var routeData = new RouteData { DataTokens = { { "area", errorArea } }, Values = { { "controller", errorController }, {"action", errorAction} } };
var controllerContext = new ControllerContext(new HttpContextWrapper(HttpContext.Current), routeData, controller);
controller.ControllerContext = controllerContext;
var sw = new StringWriter();
var razorView = new RazorView(controller.ControllerContext, pathToViewFile, "", false, null);
var model = new ViewDataDictionary(new HandleErrorInfo(exception, requestControllerName, requestActionName));
var viewContext = new ViewContext(controller.ControllerContext, razorView, model, new TempDataDictionary(), sw);
viewContext.ViewBag.ErrorMessage = errorMessage;
//TODO: add to ViewBag what you need
razorView.Render(viewContext, sw);
HttpContext.Current.Response.Write(sw);
Server.ClearError();
HttpContext.Current.Response.End(); // No more processing needed (ex: by default controller/action routing), flush the response out and raise EndRequest event.
}
View
#model HandleErrorInfo
#{
ViewBag.Title = "Error";
// TODO: SET YOUR LAYOUT
}
<div class="">
ViewBag.ErrorMessage
</div>
#if(Model != null && HttpContext.Current.IsDebuggingEnabled)
{
<div class="" style="background:khaki">
<p>
<b>Exception:</b> #Model.Exception.Message <br/>
<b>Controller:</b> #Model.ControllerName <br/>
<b>Action:</b> #Model.ActionName <br/>
</p>
<div>
<pre>
#Model.Exception.StackTrace
</pre>
</div>
</div>
}
Application_Error having issue with Ajax requests. If error handled in Action which called by Ajax - it will display your Error View inside the resulting container.
Brian,
This approach works great for non-Ajax requests, but as Lion_cl stated, if you have an error during an Ajax call, your Share/Error.aspx view (or your custom error page view) will be returned to the Ajax caller--the user will NOT be redirected to the error page.
Use Following code for redirecting on route page.
Use exception.Message instide of exception. Coz exception query string gives error if it extends the querystring length.
routeData.Values.Add("error", exception.Message);
// clear error on server
Server.ClearError();
Response.RedirectToRoute(routeData.Values);
I have problem with this error handling approach:
In case of web.config:
<customErrors mode="On"/>
The error handler is searching view Error.shtml
and the control flow step in to Application_Error global.asax only after exception
System.InvalidOperationException: The view 'Error' or its master was
not found or no view engine supports the searched locations. The
following locations were searched: ~/Views/home/Error.aspx
~/Views/home/Error.ascx ~/Views/Shared/Error.aspx
~/Views/Shared/Error.ascx ~/Views/home/Error.cshtml
~/Views/home/Error.vbhtml ~/Views/Shared/Error.cshtml
~/Views/Shared/Error.vbhtml at
System.Web.Mvc.ViewResult.FindView(ControllerContext context)
....................
So
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
httpException is always null then
customErrors mode="On"
:(
It is misleading
Then <customErrors mode="Off"/> or <customErrors mode="RemoteOnly"/> the users see customErrors html,
Then customErrors mode="On" this code is wrong too
Another problem of this code that
Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message));
Return page with code 302 instead real error code(402,403 etc)

Resources