C#MVC 3,动作超载? [英] c# mvc 3, action overloading?

查看:176
本文介绍了C#MVC 3,动作超载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to overload my index method.

下面是我的索引方法:

[ActionName("Index")]
public ActionResult IndexDefault()
{
}

[ActionName("Index")]
public ActionResult IndexWithEvent(string eventName)
{
}

[ActionName("Index")]
public ActionResult IndexWithEventAndLanguage(string eventName, string language)
{
}

这使铸造:

有关行动控制器类型CoreController'指数'当前请求是下面的操作方法之间暧昧:
System.Web.Mvc.ActionResult IndexDefault()的类型ManageMvc.Controllers.CoreController
System.Web.Mvc.ActionResult IndexWithEvent(System.String)的类型ManageMvc.Controllers.CoreController
在类型ManageMvc.Controllers.CoreController System.Web.Mvc.ActionResult IndexWithEventAndLanguage(System.String,System.String)

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

是没可能有3种不同get方法重载索引操作?

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 }
);

URL可能看起来像:

The url would look like:

本地主机/核心/索引

本地主机/ EVENT_NAME /核心/索引

localhost/event_name/Core/Index

本地主机/ EVENT_NAME /语言/核心/索引

localhost/event_name/language/Core/Index

推荐答案

重载一样,是行不通的。

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.

这篇关于C#MVC 3,动作超载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆