Asp.net mvc 模糊动作方法
我有一个指向HomeController索引的网页www.example.com。当我运行该网站时,我会获得HomeView 现在的要求是,当我键入www.example.com/Japan时,我需要运行不同的视图 我所做的:Asp.net mvc 模糊动作方法,asp.net-mvc,Asp.net Mvc,我有一个指向HomeController索引的网页www.example.com。当我运行该网站时,我会获得HomeView 现在的要求是,当我键入www.example.com/Japan时,我需要运行不同的视图 我所做的: public ActionResult Index(string country) { ViewBag.Message = "my country=" + country; return View(); } 但这给了我
public ActionResult Index(string country)
{
ViewBag.Message = "my country=" + country;
return View();
}
但这给了我一个错误:
对控制器类型“HomeController”的当前操作请求“索引”
以下操作方法之间不明确:
MVCAPApplication_2.Controllers.HomeController类型上的System.Web.Mvc.ActionResult Index()
MVCAPApplication_2.Controllers.HomeController类型上的System.Web.Mvc.ActionResult索引(System.String)
我应该做些什么来实现这个目标
我不想用
我想用
我的代码:
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)
{
//..
}
或者,您可以更改重载方法中的操作名称
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 }
);
路线
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 }
);
您可以使用AttributeRouting
[Route("Home/Index/{country}")
public ActionResult Index(string country)
{
ViewBag.Message = "my country=" + country;
switch(country)
{
case "Country1":
return View("ViewName")
}
// To do
}
你能发布你的路线定义吗?只是默认的路线定义。我没有添加任何路径。您需要在
默认路径之前按国家注册路径。切换该顺序,使默认路由为最后一个。如果我将actionmethod的名称更改为countryIndex,日本的URL将如何访问。我根据您的建议更改了一个使用httpget,另一个使用httppost,但它不起作用。当我键入“我得到”时,找不到资源。@Venkat这是因为您的请求仍然是通过该方法得到的。如果/当你改变动作名称时,你必须有另一条路线来完成。托马斯·斯特林格。在这种情况下,你能帮助写路由器吗?我没有写任何东西。只是默认值。@Venkat看看我的编辑,这是一个有效的例子。我希望这有帮助!