c#MVC3,动作重载?
我一直在试图重载我的索引方法 以下是我的索引方法:c#MVC3,动作重载?,c#,asp.net-mvc-3,overloading,C#,Asp.net Mvc 3,Overloading,我一直在试图重载我的索引方法 以下是我的索引方法: [ActionName("Index")] public ActionResult IndexDefault() { } [ActionName("Index")] public ActionResult IndexWithEvent(string eventName) { } [ActionName("Index")] public ActionResult IndexWithEventAndLanguage(string eventNa
[ActionName("Index")]
public ActionResult IndexDefault()
{
}
[ActionName("Index")]
public ActionResult IndexWithEvent(string eventName)
{
}
[ActionName("Index")]
public ActionResult IndexWithEventAndLanguage(string eventName, string language)
{
}
这将继续铸造:
当前对控制器类型“CoreController”的操作“Index”的请求在以下操作方法之间不明确:
ManageMvc.Controllers.CoreController类型上的System.Web.Mvc.ActionResult IndexDefault()
ManageMvc.Controllers.CoreController类型上的System.Web.Mvc.ActionResult IndexWithEvent(System.String)索引
类型ManageMvc.Controllers.CoreController上的System.Web.Mvc.ActionResult IndexWithEventAndLanguage(System.String,System.String)
不能用3种不同的GET方法重载index操作吗
另外,如果可能的话,正确的路线是什么?我有这个:
routes.MapRoute(
"IndexRoute", // Route name
"{eventName}/{language}/Core/{action}", // URL with parameters
new { controller = "Core", action = "Index", eventName = UrlParameter.Optional, language = UrlParameter.Optional }
);
url看起来像:
localhost/Core/Index
localhost/event\u name/Core/Index
像这样重载localhost/event\u name/language/Core/Index是行不通的 您最好的选择是使用默认值,然后将管线值设置为可选(就像您已经有了它们一样):
但是,我不确定您是否能够通过单个路线定义使路线看起来像您想要的那样。您可能必须定义三个不同的路由,并将每个路由映射回您的单一操作方法。@Patrick-因为ASP.NET MVC无法根据您定义路由的方式决定使用哪个重载(根据URL中传递的参数调用不同的重载是不够聪明的)。啊,好的,谢谢。我把它改成了你建议的方式,它开始工作了。
public ActionResult Index(string eventName = null, string language = null)
{
}