加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#mvc 3,动作重载?

发布时间:2020-12-16 05:56:08 所属栏目:百科 来源:网络整理
导读:我一直试图超载我的索引方法. 这是我的索引方法: [ActionName("Index")]public ActionResult IndexDefault(){}[ActionName("Index")]public ActionResult IndexWithEvent(string eventName){}[ActionName("Index")]public ActionResult IndexWithEventAndLa
我一直试图超载我的索引方法.

这是我的索引方法:

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

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

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

这不断投射:

控制器类型“CoreController”上的当前操作请求“Index”在以下操作方法之间是不明确的:
System.Web.Mvc.ActionResult IndexDefault()类型ManageMvc.Controllers.CoreController
System.Web.Mvc.ActionResult IndexWithEvent(System.String)on ManageMvc.Controllers.CoreController
System.Web.Mvc.ActionResult IndexWithEventAndLanguage(System.String,System.String)on ManageMvc.Controllers.CoreController

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

网址将如下所示:

本地主机/核心/索引

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

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

解决方法

这样的重载是不会奏效的.

您最好的选择是使用默认值,然后使路由值可选(如您已经拥有):

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

我不知道你会通过一条路线定义找到你想要的路线.您可能需要定义三个不同的路线,并将每个路线映射到单个Action方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读