Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,

I am working on a MVC application, here whenever the error occurred it should redirected to error page.
While running in my local machine it works as expected.
But when in deployed solution, its throwing web.config warning to change the "Custom error mode=off" so that you can view the error something like that.

Is there any solution to solve this above issue?
What I want is I need to redirect to error page once error is occurred in the application.

Thanks,
RK
Posted
Comments
[no name] 31-Jul-13 6:18am    
"Is there any solution to solve this above issue", yes there is. Read the message, do what it says, get the real error, fix it.
♥…ЯҠ…♥ 31-Jul-13 6:23am    
Am not asking here to fix the issue, I want to redirect to error page and to show that error in that.Do I make sense?
[no name] 31-Jul-13 7:01am    
No that does not make any sense. You have to fix your code first.

HI,
create your error controler and view.
then, in web.config in <system.web> section add

XML
<customErrors mode="On">
      <error statusCode="404" redirect="~/Error/Error"/>
    </customErrors>
 
Share this answer
 
Comments
♥…ЯҠ…♥ 31-Jul-13 8:36am    
Thanks for you response, as of now if error occurs it is redirected to error page in my local machine.It fails to redirect to error page in deployed machine.Is there something to do with config file?
Please follow the steps.this is the custom error handling which helps you display the exception in your error page
Create a Controller and named it as Error controller and the actionresults like below
C#
public class ErrorController : Controller
    {
        public ActionResult Index()
        {
            var error = ViewData.Model;
            return View(error);
        }

    }


Create view by right clicking the index view
C#
@model HandleErrorInfo
@{
    ViewBag.Title = "Server Error";
}

XML
<h2 class="errorh">Server Error</h2>
<div>Controller: @Model.ControllerName</div>
<div>Action Name : @Model.ActionName</div>
<div>Exception: @Model.Exception.Message</div>
<div id="moreDetails">
<div>Stack Trace: @Model.Exception.StackTrace</div>
<div>Inner Exception: @Model.Exception.InnerException</div>
</div>


put the below in below in application_error method in the Global.asax file
C#
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 controller = new ErrorController();
           var routeData = new RouteData();
           var action = "Index";

           if (ex is HttpException)
           {
               var httpEx = ex as HttpException;

               switch (httpEx.GetHttpCode())
               {
                   case 404:
                       action = "NotFound";
                       break;

                   case 401:
                       action = "AccessDenied";
                       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));
       }

Hope this helps
 
Share this answer
 
v2
Comments
Jameel VM 31-Jul-13 8:36am    
whenever any exception happens in your application it will automatically redirect in to the error page
♥…ЯҠ…♥ 31-Jul-13 8:37am    
Thanks for you response, as of now if error occurs it is redirected to error page in my local machine.It fails to redirect to error page in deployed machine.Is there something to do with config file?
Jameel VM 31-Jul-13 10:28am    
where did you set the redirection url of the error message?
♥…ЯҠ…♥ 1-Aug-13 1:18am    
I have added <customerrors mode="On"> only in web.config file. It works fine in local machine, it fails in deployed machine.
Jameel VM 1-Aug-13 1:23am    
can you share the part of web config file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900