0
votes

I'm working on a multi language web site and I need to set language first and then shows the page in that exact language using Resources files.

I used two index action like this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using global_vrf.GeoIpService;

 namespace global_vrf.Controllers
  {
    public class HomeController : Controller
    {
       public ActionResult Index(string language)
    {


        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);

        return View();
    }

    public ActionResult Index() 
    {
        string language="en-us";

        return View(language);


    }


  }
}

but when I run the page, I have this Error:

The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index(System.String) on type global_vrf.Controllers.HomeController System.Web.Mvc.ActionResult Index() on type global_vrf.Controllers.HomeController

2
this occurs because you have same name action methods. To avoid this you have to use [HTTPGet] and [HTTPPost] before each methods.Kate Fernando
Delete the 2nd method. in the first method, if the value of language is null, the set it to "en-us"user3559349
@StephenMueckethank you so much, teo van kot suggested the same and that works fineneda Derakhshesh

2 Answers

1
votes

Just make one method:

 namespace global_vrf.Controllers
 {
    public class HomeController : Controller
    {
       public ActionResult Index(string language)
       {
          if(String.IsNullOrWhiteSpace(language))
          {
             string language="en-us";
          }
          Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
          Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);

          return View();
       }
    }
 }

You can't make 2 methods becouse string could be null.

0
votes
[HttpPost]
public ActionResult Index(string language)
{
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);

    return View();
}

[HttpGet]
    public ActionResult Index() 
    {
        string language="en-us";

        return View(language);


    }