Reputation: 15140
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!
Upvotes: 0
Views: 459
Reputation: 2542
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");
Upvotes: 0
Reputation: 2123
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.
Upvotes: 1
Reputation: 25684
You can get access to the response from the HttpContext.
HttpContext.Current.Response
Upvotes: 1