C#MVC 3,动作超载?

编程入门 行业动态 更新时间:2024-10-11 21:24:17
本文介绍了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.CoreControllerSystem.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,动作超载?

本文发布于:2023-10-12 04:04:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动作   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!