gpt4 book ai didi

asp.net-mvc - 行动方法不明确

转载 作者:行者123 更新时间:2023-12-01 21:56:57 26 4
gpt4 key购买 nike

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

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

我做了什么:

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

return View();
}

但它给了我一个错误:

The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods:

System.Web.Mvc.ActionResult Index() on type MvcApplication_2.Controllers.HomeController System.Web.Mvc.ActionResult Index(System.String) on type MvcApplication_2.Controllers.HomeController

我应该做什么来实现这一目标?

我不想使用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();
}
}

最佳答案

同一个操作名称不能使用相同的 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 }
);

关于asp.net-mvc - 行动方法不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31788896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com