c# mvc 3,动作重载?
我一直在尝试重载我的索引方法。
这是我的索引方法:
[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 方法重载索引操作?
另外,如果可以的话,正确的路线是什么?我有这个:
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_name/Core/Index
localhost/event_name/language/Core/Index
I have been trying to overload my index method.
Here are my index methods:
[ActionName("Index")]
public ActionResult IndexDefault()
{
}
[ActionName("Index")]
public ActionResult IndexWithEvent(string eventName)
{
}
[ActionName("Index")]
public ActionResult IndexWithEventAndLanguage(string eventName, string language)
{
}
This keeps casting:
The current request for action 'Index' on controller type 'CoreController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult IndexDefault() on type ManageMvc.Controllers.CoreController
System.Web.Mvc.ActionResult IndexWithEvent(System.String) on type ManageMvc.Controllers.CoreController
System.Web.Mvc.ActionResult IndexWithEventAndLanguage(System.String, System.String) on type ManageMvc.Controllers.CoreController
Is it not possible to overload the index action with 3 different GET methods?
Also, if it is possible, what would be the correct route? I have this:
routes.MapRoute(
"IndexRoute", // Route name
"{eventName}/{language}/Core/{action}", // URL with parameters
new { controller = "Core", action = "Index", eventName = UrlParameter.Optional, language = UrlParameter.Optional }
);
The url would look like:
localhost/Core/Index
localhost/event_name/Core/Index
localhost/event_name/language/Core/Index
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样的超载是行不通的。
最好的选择是使用默认值,然后将路由值设置为可选(就像您已经拥有它们一样):
不过,我不确定您是否会通过单个路由定义让路由看起来像您想要的那样。您可能必须定义三个不同的路由并将每个路由映射回您的单个操作方法。
Overloading like that isn't going to work.
Your best option is to use default values and then making the route values optional (like you already have them):
I'm not sure you're going to get the route to look the way you want with a single route definition though. You're probably going to have to define three different routes and map each back to your single Action method.