I am working on Asp.net mvc3 application.For exception handling i using following code in global.asax
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
Server.ClearError();
Response.Redirect("/Secure/Error", false);
}
if any exception occur it will redirect to Error page.I want to some exeception should not redirect to Error page.
Like if any user enter following url
there is no controller of ‘index‘ name it will be throw exception
The controller for path ‘/index’ was not found or does not implement
IController
I want this when this exeception occur it should redirect to http://www.example.com
How can i do this?
There are some other keyword like index.. that should be redirect to main url of website
If you want to do that only for specific keywords, why not add corresponding routes instead of handling the 404 – Not Found exceptions?
You could do this in your Globals.asax.cs file, next to the other route definitions:
and likewise for all keywords other than index.
Update
After making /index and the other urls point to
HomeController.RedirectToRoot()(or another controller/action of your choice), just implement it to return a RedirectResult that would take the user to the site’s root:Update 2
If you are absolutely sure that you’ll never use /index or the other special urls for anything but redirecting to root, then you could do a permanent redirect instead:
This will save some round trips to the server, but the redirection will be permanently stored in client browsers, making it very hard/impossible to use those urls for anything else in the future.