0

How to write one method and pass argument and call it in each controller? The problem: I setup my localization basically on my RouteConfig file:

 routes.MapRoute(
            name: "Default",
            url: "{language}/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, language = "en-US" }
        );

And when user changes languages, on Url.ActionLink I am sending the parameter "languageCulture" (for example: "en-US").

Because I create a website for my private purposes, I created each method in Home controller will be a separate web page. And now I ended up with duplicated code:

    public class HomeController : BaseController
    {
    GlobalModel gm;
    public HomeController()
    {
        gm = new GlobalModel();
    }

    public ActionResult Index(string language)
    {
        try
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
            gm.CultureCode = language;
            return View(gm);
        }
        catch (System.Exception ex)
        {

            throw ex;
        }
    }
    public ActionResult Services(string language)
    {
        try
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
            gm.CultureCode = language;
            return View(gm);
        }
        catch (System.Exception ex)
        {

            throw ex;
        }
    }
}

I created and a GLobal model class that holds information about culture. I need that, because I want to colorize language label based on that "flag" from jQuery.

My questiuon is: How can I write method in BaseController for example:

public class BaseController : Controller
{
   public ActionResult SetCulture(string language)
    {
       //implementation something like
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
        GlobalModel gm = new GLobalModel();
        gm.CultureCode = language;

        return RedirectToAction();
    }
}

How can I from this method call specific method in Home controller? Am I need to pass additional parameter which method I want to call or is there some "simpler" solution?

Stefan0309
  • 1,602
  • 5
  • 23
  • 61
  • 1
    Side note: Your `catch` blocks are not only superfluous, they're actually detrimental because they remove useful information about the exceptions they catch. You're better off just removing them entirely. – David Dec 08 '18 at 19:30
  • @David thanks. I also think that is a good idea! – Stefan0309 Dec 08 '18 at 19:38
  • Instead of extending `Controller`, extend `ActionResult`. – Jasen Dec 08 '18 at 19:44
  • @Jasen It makes sense. Could you post an answer with some mini example? Thanks – Stefan0309 Dec 08 '18 at 19:49
  • various method to set cultures, here :- https://stackoverflow.com/questions/7216057/setting-culture-for-asp-net-mvc-application-on-vs-dev-server-and-iis – Yash Soni Dec 08 '18 at 20:45

0 Answers0