模棱两可的行动方法

时间:2015-08-03 13:40:04

标签: asp.net-mvc

我有一个网页www.example.com,它指向一个HomeController索引。当我运行网站时,我得到了HomeView。

现在需要的是当我输入www.example.com/Japan时,我需要运行不同的视图。

我做了什么:

 public ActionResult Index(string country)
    {
        ViewBag.Message = "my country=" + country;

        return View();
    }

但它给了我一个错误:

  

控制器类型'HomeController'上的当前操作请求'Index'        以下操作方法之间存在歧义:

     

类型为MvcApplication_2.Controllers.HomeController的System.Web.Mvc.ActionResult Index()        MvcApplication_2.Controllers.HomeController类型的System.Web.Mvc.ActionResult索引(System.String)

我应该怎么做才能实现这个目标?

我不想使用http://example.com/country/japan

我想使用http://example.com/japan

我的代码: RouteConfig.cs

 namespace MvcApplication_2
 {
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        routes.MapRoute(
        name: "ByCountry",
        url: "{country}",
        defaults: new { controller = "Home", action = "IndexByCountry" }
    );


    }
}

}

Homecontroller.cs

 public class HomeController : Controller
 {
    [HttpGet]
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }


    [ActionName("IndexByCountry")]
    public ActionResult Index(string country)
    {
        ViewBag.Message = "Japan man.";

        return View("Index");
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

2 个答案:

答案 0 :(得分:2)

对于相同的操作名称,您不能使用相同的HTTP谓词。换句话说,对于相同的操作名称,即使是重载也不可能HttpGet

您有三种选择:

将一个或您的操作方法更改为其他HTTP操作动词...

[HttpGet]
public ActionResult Index()
{
    //..
}

[HttpPost]
public ActionResult Index(string country)
{
    //..
}

或者,更改操作方法的名称......

public ActionResult CountryIndex(string country)
{
    ViewBag.Message = "my country=" + country;

    return View();
}

或者,您可以从重载方法更改操作名称...

public ActionResult Index()
{
    //..
}

[ActionName("IndexByCountryName")]
public ActionResult Index(string country)
{
    //..
}

工作示例

这使用最后一个选项,保持方法名称重载但为重载指定ActionNameAttribute

操作

    public ActionResult Index()
    {
        ViewBag.Message = "no country selected!";

        return View();
    }

    [ActionName("IndexByCountry")]
    public ActionResult Index(string country)
    {
        ViewBag.Message = string.Format("County selected :: {0}", country);

        // the below ActionResult reuses the Index view but 
        // here you could have a separate view for the country 
        // selection if you like
        //
        return View("Index");
    }

<强>路线

        routes.MapRoute(
            name: "ByCountry",
            url: "{country}",
            defaults: new { controller = "Home", action = "IndexByCountry" }
        );

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

答案 1 :(得分:1)

您可以使用AttributeRouting。

[Route("Home/Index/{country}")
public ActionResult Index(string country)
{
    ViewBag.Message = "my country=" + country;

    switch(country)
    {
        case "Country1":
             return View("ViewName")
    }

    // To do
}