Avoid handle 404 on other requests than main - c#

I used the method below in the EndRequest method from global asax. I get a HTTP 404 error even from image request or other requests. The point is that I want to check only for the first request/main request. Can you help me please. This is the only solution that works.
{
if (Context.Response.StatusCode == 404)
{
Response.Clear();
var rd = new RouteData();
rd.DataTokens["area"] = "AreaName";
rd.Values["controller"] = "Errors";
rd.Values["action"] = "NotFound";
IController c = new ErrorsController();
c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
}
}

Assuming you want to only handle 404s for controller actions you can check whether or not the current request is for one like this:
if(!string.IsNullOrEmpty(Request.RequestContext.RouteData.GetRequiredString("controller")))
{
...handling code goes here....
}

Related

Custom Error Handler

I have searched the web and stitched together an error handling solution that doesn't work mainly because I don't understand fully how the exception pipeline works. I used different guides, but I didn't get any of theme to work for me. What I want the error handler to do is this. I have a class called workplanRepo where all my queries are executed. I have covered all queries with a try and catch block. What I want is when an error occurs is for an exception to be thrown that allows me to customize a specific message for each query and the default exception message. I then want to be able to retrieve the messages in the error view that the exception handler has redirected the user to. I would also like a default handler that catches all other errors. but don't necessarily have the custom message part. If anybody could explain or show me how I can achieve this. I would be very grateful!. This is one of the query methods:
try {
newItem["Author"] = _user.Id;
newItem["Title"] = _user.Title;
newItem.Update();
clientContext.ExecuteQuery();
}
catch (Exception e) {
throw new HttpException("Oops, there must have been an error: " + e.Message);
}
In ASP.NET MVC 5, we can catch error inside Global.asax.cs's Application_Error event instead of using try catch block in every query. From then redirect to custom error page.
In addition, we can also use logging framework like Log4Net and NLog.
For example,
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
LogException(exception);
if (exception is HttpAntiForgeryException)
{
Response.Clear();
Server.ClearError();
Response.TrySkipIisCustomErrors = true;
// Call target Controller and pass the routeData.
IController controller = EngineContext.Current.Locator.GetInstance<CommonController>();
var routeData = new RouteData();
routeData.Values.Add("controller", "Common");
routeData.Values.Add("action", "AntiForgery");
var requestContext = new RequestContext(new HttpContextWrapper(Context), routeData);
controller.Execute(requestContext);
}
else
{
// Process 404 HTTP errors
var httpException = exception as HttpException;
if (httpException != null && httpException.GetHttpCode() == 404)
{
Response.Clear();
Server.ClearError();
Response.TrySkipIisCustomErrors = true;
// Call target Controller and pass the routeData.
IController controller = EngineContext.Current.Locator.GetInstance<CommonController>();
var routeData = new RouteData();
routeData.Values.Add("controller", "Common");
routeData.Values.Add("action", "PageNotFound");
var requestContext = new RequestContext(new HttpContextWrapper(Context), routeData);
controller.Execute(requestContext);
}
}
}
private void LogException(Exception ex)
{
if (ex == null)
return;
// Ignore 404 HTTP errors
var httpException = ex as HttpException;
if (httpException != null &&
httpException.GetHttpCode() == 404)
return;
try
{
// Log error message
}
catch (Exception)
{
// Don't throw new exception if occurs
}
}
You can view sample project which use Log4Net at GitHub.

Do "if" in Application WebConfig if it is posible

I want to do like this -
but I cent rite razor by web config.
Is there a way to write razor or do it in a different way to my goal will only manager see the errors
Apologize in advance for my English
#using Or50Core.Controllers;
#if ((BaseController)this.ViewContext.Controller).IsAdministrator())
{
<system.web>
<customErrors mode="Off"></customErrors>
</system.web>
}else{
<system.web>
<customErrors mode="On"></customErrors>
</system.web>
}
"if" do the work in views
you would be far better off using logging. That way you catch all the errors (not just ones the administrator gets) in the log files/DB but the users only get a friendly error.
You can use this code:
#{
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");
if (section != null)
{
#if ((BaseController)this.ViewContext.Controller).IsAdministrator())
{
section.Mode = CustomErrorsMode.Off;
}
else
{
section.Mode = CustomErrorsMode.On;
}
}
configuration.Save();
}
this code needs to add #using System.Web.Configuration; to view.
Edit:
For manage users you can use ASP.NET Identity and for manage error page you can use Custom Error Page.
You have to write Application_Error method in your Global.ascx. In this method you can check if current user is in Admin role or not and based on that you can show the real error or just a simple error page.
protected void Application_Error()
{
if (!User.IsInRole("Administrator"))
{
var exception = Server.GetLastError();
var httpException = exception as HttpException;
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Errors";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
Response.StatusCode = 500;
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 403:
routeData.Values["action"] = "Http403";
break;
case 404:
routeData.Values["action"] = "Http404";
break;
}
}
IController errorsController = new ErrorsController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}
}
Here you determine what users see based on each error
public class ErrorsController : Controller
{
public ActionResult General(Exception exception)
{
return Content("General failure", "text/plain");
}
public ActionResult Http404()
{
return Content("Not found", "text/plain");
}
public ActionResult Http403()
{
return Content("Forbidden", "text/plain");
}
}
BTW I find the answer in Here

Direct to action in lieu of 404 page

I'm making a single-page-application and the way I have it set up is that using a base controller in the OnActionExecuting() method I redirect non-ajax requests to the home index action. The path will still be there and used as an indicator to tell the javascript what to do.
That works fine for something like /login where there is actually a /login page that would normally be accessible if I hadn't blocked it using the technique I mentioned.
However when I take it a step further and use /some/other/meaningful/but/bogus/url which has no route/controller but has some meaning to the javascript, I get a 404 error.
So obviously what I'd like to do, is in that 404 situation I would like to just load the home index action instead. Alternatively a replacement for my ajax blocking that redirects all paths to the home index action (unless they're valid routes called with ajax) would have the same result.
I've searched for similar answers, unfortunately the same wording is frequently used to describe questions regarding custom 404 pages, so it's a tough one to search for.
You do want a custom error page. The difference is your custom error page is a controller/action. A custom error page doesn't have to be a page like notfound.html it is just a url so you can set the url to a controller action.
The code below will redirect unhandled status codes to "/home" i.e. the home controller. Then for 404 it will go to /controller/action which could be /home/notfound.
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="/home">
<error statusCode="404" redirect="/controller/action"/>
</customErrors>
</system.web>
The non-redirect route, in your global.asax.cs add the following code. This example maybe a little heavy but essentially achieves what you want in that the custom error controller is executed but the URL is unchanged.
protected void Application_Error(object sender, EventArgs e)
{
var httpContext = ((MvcApplication)sender).Context;
var currentController = " ";
var currentAction = " ";
var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
if (currentRouteData != null)
{
if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
{
currentController = currentRouteData.Values["controller"].ToString();
}
if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
{
currentAction = currentRouteData.Values["action"].ToString();
}
}
var ex = Server.GetLastError();
var controllerFactory = ControllerBuilder.Current.GetControllerFactory();
var controller = (Controller)controllerFactory.CreateController(httpContext.Request.RequestContext, "Error");
var routeData = new RouteData();
var action = "Index";
if (ex is HttpException)
{
var httpEx = ex as HttpException;
switch (httpEx.GetHttpCode())
{
case 401:
action = "Unauthorized";
break;
case 403:
action = "Forbidden";
break;
case 404:
action = "NotFound";
break;
default:
break;
}
}
httpContext.ClearError();
httpContext.Response.Clear();
httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
httpContext.Response.TrySkipIisCustomErrors = true;
routeData.Values["controller"] = "Error";
routeData.Values["action"] = action;
controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
}

Application_Error renders page on local host but not on production(www)

I have an Application_Error which renders an error page:
private void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
// A good location for any error logging, otherwise, do it inside of the error controller.
Response.Clear();
HttpException httpException = exception as HttpException;
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
// Clear the error, otherwise, we will always get the default error page.
Server.ClearError();
routeData.Values.Add("id", httpException.GetHttpCode());
Response.StatusCode = httpException.GetHttpCode();
Context.Response.ContentType = "text/html";
// Call the controller with the route
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
works on my local machine seamlessly. when i push it to production, it renders the regular asp 404 page instead of the page which the controller is supposed to server.
Even on production server, when i use localhost instead of www it works.
i should add that i have no configrations in web.config for customerrors. i tried turning them off and that didnt help either.
to me it seems like it is the url that is causing this but cant figure out why/how.
Any idea how to solve the problem?
In your Application_Error method please try adding this...
Response.TrySkipIisCustomErrors = true;

How do I create a global custom error page for ASP.NET MVC3?

I brand new to ASP.NET MVC3. How would I create a global custom error page for MVC3? The general idea is when an exception is thrown it would show a generic message to the user and it would log the exception to a database for developers to investigate later.
Thanks in advance for your help.
Here is what I ended up doing in global.asax.cs:
protected void Application_Error()
{
var exception = Server.GetLastError();
Log.Error("Exception", exception);
var httpException = exception as HttpException;
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Error";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
Response.StatusCode = 500;
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 403:
routeData.Values["action"] = "Http403";
break;
case 404:
routeData.Values["action"] = "Http404";
break;
}
}
IController errorsController = new ErrorController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}
In your Global.asax file implement the Application_Error method:
protected void Application_Error() {
HttpContext ctx = HttpContext.Current;
var error = ctx.Server.GetLastError();
ctx.Response.Clear();
ctx.Response.End();
}
Following up on Maess' comment:
Read this: Error Handling in asp.net mvc 3
What I've done in my project is I created a BaseController and overridden the OnException event as below,
protected override void OnException(ExceptionContext filterContext)
{
// do some logging using log4net or signal to ELMAH
filterContext.ExceptionHandled = true;
var exModel = new HandleErrorInfo(filterContext.Exception,
filterContext.RouteData.Values["controller"].ToString(),
filterContext.RouteData.Values["action"].ToString());
View("Error", exModel).ExecuteResult(ControllerContext);
}
Also I removed the HandleError action filter registered in the Global.asax.cs.
Note: You should have a view with name Error in shared folder.
Update: To extract the error information from the Error view you have to bind the Error view to the model HandleErrorInfo.
#model System.Web.Mvc.HandleErrorInfo
Then you can easily access the exception anywhere in the view as
#Model.Exception
Create a view called Error and add it to your Views\Shared folder. Then in your catch blocks, after you have logged the error, redirect the action to the error view. If you want to display a sophisticated message create an Error model and pass it to the view, or put the information into the ViewBag.
For information on unhandled exceptions and HTTP errors see Ropstah's post.

Categories

Resources