【发布时间】:2017-11-02 10:37:57
【问题描述】:
我正在开发一个多语言网站,我需要先设置语言,然后使用资源文件以该语言显示页面。
我使用了两个这样的索引操作:
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);
}
}
}
但是当我运行页面时,我有这个错误:
当前对控制器类型的操作“索引”请求 'HomeController' 在以下操作方法之间不明确: System.Web.Mvc.ActionResult Index(System.String) 类型 global_vrf.Controllers.HomeController System.Web.Mvc.ActionResult 类型 global_vrf.Controllers.HomeController 上的 Index()
【问题讨论】:
-
发生这种情况是因为您有相同的名称操作方法。为避免这种情况,您必须在每个方法之前使用 [HTTPGet] 和 [HTTPPost]。
-
删除第二种方法。第一种方法,如果
language的值为null,则设置为"en-us" -
@StephenMuecketthank you so much, teo van kot 提出了同样的建议,效果很好