locked
base controller help RRS feed

  • Question

  • User1034446946 posted

    Ages ago someone posted a base controller, which had loads of things in it, like localization and a lot more, I thought it was in one of my threads but for the life of me i cant find it, if anyone as one i could reference i would appriciate it

    Thursday, February 14, 2019 2:09 AM

All replies

  • User1120430333 posted

    This base controller deals with global exception handling in a MVC 5 solution. You can look at how it Is used as an example of using a base controller, which I implement in controllers where needed.

    https://www.code-sample.com/2014/08/base-controller-in-mvc-5.html

    Thursday, February 14, 2019 6:53 PM
  • User1034446946 posted

    Thanks for the link, but the one posted on the forum had much more included.

    Friday, February 15, 2019 2:59 PM
  • User1520731567 posted

    Hi EnenDaveyBoy,

    Ages ago someone posted a base controller, which had loads of things in it, like localization and a lot more, I thought it was in one of my threads but for the life of me i cant find it, if anyone as one i could reference i would appriciate it

    Sorry,I did't fine the most suitable base controller for you.

    But I find some articles about localization & internationalization in base controller,you could refer to:

    move the localization logic to the Controller.ExecuteCode method,and then overrided the ExecuteCode method with the localization logic

    public abstract class BaseController : Controller
         {
             protected override void ExecuteCore()
             {
                 if (RouteData.Values["lang"] != null &&
                     !string.IsNullOrWhiteSpace(RouteData.Values["lang"].ToString()))
                 {
                     // set the culture from the route data (url)
                     var lang = RouteData.Values["lang"].ToString();
                     Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
                 }
                 else
                 {
                     // load the culture info from the cookie
                     var cookie = HttpContext.Request.Cookies["ShaunXu.MvcLocalization.CurrentUICulture"];
                     var langHeader = string.Empty;
                    if (cookie != null)
                     {
                         // set the culture by the cookie content
                         langHeader = cookie.Value;
                         Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
                     }
                     else
                     {
                         // set the culture by the location if not speicified
                         langHeader = HttpContext.Request.UserLanguages[0];
                         Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
                     }
                     // set the lang value into route data
                     RouteData.Values["lang"] = langHeader;
                 }
      
                 // save the location into cookie
                 HttpCookie _cookie = new HttpCookie("ShaunXu.MvcLocalization.CurrentUICulture", Thread.CurrentThread.CurrentUICulture.Name);
                 _cookie.Expires = DateTime.Now.AddYears(1);
                 HttpContext.Response.SetCookie(_cookie);
      
                 base.ExecuteCore();
             }
         }

    http://blogs.shaunxu.me/archive/2010/05/06/localization-in-asp.net-mvc-ndash-3-days-investigation-1-day.aspx

    And Internationalization in base controller:

    https://afana.me/archive/2011/08/04/aspnet-mvc-i18n-part-2.aspx/

    Best Regards.

    Yuki Tao

    Monday, February 18, 2019 3:19 AM