c# mvc 3,动作重载?

发布于 2024-11-30 05:37:40 字数 1211 浏览 0 评论 0原文

我一直在尝试重载我的索引方法。

这是我的索引方法:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿! 2024-12-07 05:37:40

这样的超载是行不通的。

最好的选择是使用默认值,然后将路由值设置为可选(就像您已经拥有它们一样):

public ActionResult Index(string eventName = null, string language = null)
{
}

不过,我不确定您是否会通过单个路由定义让路由看起来像您想要的那样。您可能必须定义三个不同的路由并将每个路由映射回您的单个操作方法。

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):

public ActionResult Index(string eventName = null, string language = null)
{
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文