0

I`m doing custom error handling.
My code:
Global.asax:

 public void Application_Error(Object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Server.ClearError();

        var routeData = new RouteData();
        routeData.Values.Add("controller", "ErrorPage");
        routeData.Values.Add("action", "Error");
        routeData.Values.Add("exception", new HandleErrorInfo(exception, "ErrorPage", "Error"));

        if (exception.GetType() == typeof(HttpException))
        {
            routeData.Values.Add("statusCode", ((HttpException)exception).GetHttpCode());
        }
        else
        {
            routeData.Values.Add("statusCode", 500);
        }

        Response.TrySkipIisCustomErrors = true;
        var controller = new ErrorPageController();

        ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        Response.End();
    }

Controller:

public class ErrorPageController : Controller
{
    public ActionResult Error(int statusCode, HandleErrorInfo exception)
    {
        Response.StatusCode = statusCode;
        ViewBag.StatusCode = statusCode.ToString();
        return View(exception);
    }
}

View:

@model System.Web.Mvc.HandleErrorInfo
@{
    ViewBag.Title = Resource.Error+ " " + (!String.IsNullOrEmpty(ViewBag.StatusCode)) ? ViewBag.StatusCode : "500" ;
}

<h1 class="error">@(Resource.Error + " " + (!String.IsNullOrEmpty(ViewBag.StatusCode)) ? ViewBag.StatusCode : "500" +". "+ Resource.Sorry):</h1>
<h4><a href ="@Url.Action("Index","Home")"><i>@Resource.MainPage</i></a></h4>

This is basically slightly changed this answer.
If my view contains any Resource values, browser only gets error code, without page. If I remove all of them, works just fine. Any ideas how make view work with resouces?
Also, should controller from Application_Error be disposed in the method?

Community
  • 1
  • 1
Cute pumpkin
  • 342
  • 3
  • 17
  • 1
    Sometimes resource doesn't appear if file is not completely full. For example if your resource.rsx file has 20 entries then resource.lang.resx must have the same 20 entries. It doesn't matter that you don't use it at the time – Daniil T. Feb 18 '17 at 02:02
  • @DaniilT. got it. Maybe will try to fully fill files later. In this case, is it possible that resource values don't work only for one view? – Cute pumpkin Feb 19 '17 at 12:24
  • I put my answer. Probably... I'm not quite sure. But I have kind of situation that localization didn't appear when resource not full in all resource files. – Daniil T. Feb 20 '17 at 15:30

1 Answers1

0

Sometimes resource doesn't appear if file is not completely full. For example if your resource file has 20 entries then resource.lang.resx must have the same 20 entries. It doesn't matter that you don't use it at the time

Daniil T.
  • 1,145
  • 2
  • 13
  • 33