I created a new project in Microsoft Visual Studio Express 2013 for Web. It is an ASP.NET MVC 5 - .NET Framework 4.5 project.
I wanted to handle (The resource cannot be found):
I did handle it using the code below.
This code will work if I do something like (/Home/kddiede/ddiij) or (/djdied/djie/djs), this will result in showing my custom Error page.
However, when I try to do something like (/Home/kddiede/ddiij/dfd/sdfds/dsf/dsfds/fd), or any long non-existing URL, It will show me this:
Code from: http://www.codeproject.com/Articles/635324/Another-set-of-ASP-NET-MVC-4-tips
Tip 16: Customizing error screens
Error Page is in the /View/Shared/Error.cshtml
Web.config
<system.web>
<customErrors mode="RemoteOnly" />
</system.web>
Global.asax
protected void Application_EndRequest(Object sender, EventArgs e)
{
ErrorConfig.Handle(Context);
}
ErrorConfig Class
public class ErrorConfig
{
public static void Handle(HttpContext context)
{
switch (context.Response.StatusCode)
{
//Not authorized
case 401:
Show(context, 401);
break;
//Not found
case 404:
Show(context, 404);
break;
}
}
static void Show(HttpContext context, Int32 code)
{
context.Response.Clear();
var w = new HttpContextWrapper(context);
var c = new ErrorController() as IController;
var rd = new RouteData();
rd.Values["controller"] = "Error";
rd.Values["action"] = "Index";
rd.Values["id"] = code.ToString();
c.Execute(new RequestContext(w, rd));
}
}
ErrorController
internal class ErrorController : Controller
{
[HttpGet]
public ViewResult Index(Int32? id)
{
var statusCode = id.HasValue ? id.Value : 500;
var error = new HandleErrorInfo(new Exception("An exception with error " + statusCode + " occurred!"), "Error", "Index");
return View("Error", error);
}
}
This last bit of code from the website I mentioned above was not added in Global.asax because It is already in FilterConfig.cs
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
Anyone knows how to fix it?
Thanks in advance.
Solved. To point all non-existing urls to your error page, do the following:
Add the code below at the end of your RouteConfig.cs file:
public static void RegisterRoutes(RouteCollection routes)
{
// Default
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// Add this code to handle non-existing urls
routes.MapRoute(
name: "404-PageNotFound",
// This will handle any non-existing urls
url: "{*url}",
// "Shared" is the name of your error controller, and "Error" is the action/page
// that handles all your custom errors
defaults: new { controller = "Shared", action = "Error" }
);
}
Add the code below to your Web.config file:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules>
</system.webServer>
<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />
</system.web>
</configuration>
That should point all the non-existing urls such as (/ad/asd/sa/das,d/asd,asd.asd+dpwd'=12=2e-21) to your error page.
Another approach would be to add this to your web.config inside of the system.web
element
<system.web>
<!-- ... -->
<!--Handle application exceptions-->
<customErrors mode="On">
<!--Avoid YSOD on 404/403 errors like this because [HandleErrors] does not catch them-->
<error statusCode="404" redirect="Error/Index" />
<error statusCode="403" redirect="Error/Index" />
</customErrors>
</system.web>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With