我有一个指向 HomeController 索引的网页 www.example.com。当我运行网站时,我得到了 HomeView。
现在的要求是,当我输入 www.example.com/Japan 时,我需要运行不同的视图。
我做了什么:
public ActionResult Index(string country)
{
ViewBag.Message = "my country=" + country;
return View();
}
Run Code Online (Sandbox Code Playgroud)
但它给了我一个错误:
当前对控制器类型“HomeController”的操作“Index”的请求在以下操作方法之间不明确:
System.Web.Mvc.ActionResult Index() 在 MvcApplication_2.Controllers.HomeController 类型上 System.Web.Mvc.ActionResult Index(System.String) 在 MvcApplication_2.Controllers.HomeController 类型上
我应该怎么做才能实现这一目标?
我不想使用http://example.com/country/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" }
);
}
}
Run Code Online (Sandbox Code Playgroud)
}
家庭控制器.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();
}
}
Run Code Online (Sandbox Code Playgroud)
不能为相同的操作名称使用相同的 HTTP 动词。换句话说,HttpGet不可能有相同的动作名称,甚至是重载。
您有三个选择:
将一个或您的操作方法更改为不同的 HTTP 操作动词...
[HttpGet]
public ActionResult Index()
{
//..
}
[HttpPost]
public ActionResult Index(string country)
{
//..
}
Run Code Online (Sandbox Code Playgroud)
或者,更改您的操作方法的名称...
public ActionResult CountryIndex(string country)
{
ViewBag.Message = "my country=" + country;
return View();
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以从重载的方法中更改操作名称...
public ActionResult Index()
{
//..
}
[ActionName("IndexByCountryName")]
public ActionResult Index(string country)
{
//..
}
Run Code Online (Sandbox Code Playgroud)
这使用最后一个选项,保持方法名称重载但指定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");
}
Run Code Online (Sandbox Code Playgroud)
路线
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 }
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2539 次 |
| 最近记录: |