简体   繁体   English

控制器中的MVC5模糊动作方法

[英]MVC5 ambiguous action methods in controller

I have coded a C# MVC5 Internet application and have a question about two ActionResult methods in the same controller. 我已经编写了一个C#MVC5 Internet应用程序,并对同一个控制器中的两个ActionResult方法有疑问。

I have two Index ActionResult methods as follows: 我有两个Index ActionResult方法,如下所示:

public async Task<ActionResult> Index()

public async Task<ActionResult> Index(int? mapCompanyId)

I am wanting to browse to either the Index() method or the Index(int? mapCompanyId) method, depending on if a value is specified for the mapCompanyId. 我想浏览到Index()方法还是Index(int?mapCompanyId)方法,这取决于是否为mapCompanyId指定了值。

Currently, I am getting this error: 目前,我收到此错误:

The current request for action 'Index' on controller type 'MapLocationController' is ambiguous between the following action methods:
System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult] Index() on type CanFindLocation.Controllers.MapLocationController
System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult] Index(System.Nullable`1[System.Int32]) on type CanFindLocation.Controllers.MapLocationController

I can rewrite my code so that there is only one Index ActionResult, but would rather have two if possible. 我可以重写代码,以便只有一个Index ActionResult,但如果可能的话,宁愿有两个。

Is it possible to have two ActionResults with the same name, and depending on if a value is specified, the relevant ActionResult is executed. 是否可以有两个具有相同名称的ActionResult,并且根据是否指定了值来执行相关的ActionResult。 If so, is it easy to implement, or is it not worth the time? 如果是这样,是否易于实施?还是不值得花时间?

This isn't possible since you're trying to do a GET request for each method. 这是不可能的,因为您正在尝试为每种方法执行GET请求。 The ASP.NET MVC action selector doesn't know which method to pick. ASP.NET MVC操作选择器不知道选择哪种方法。

If it makes sense and you're able you can use the HttpGet or HttpPost attributes to differentiate the method for each type of HTTP request. 如果可行,则可以使用HttpGetHttpPost属性来区分每种HTTP请求类型的方法。 It doesn't sound like that makes sense in your case. 在您的情况下,听起来似乎没有道理。

You can assign different HTTP methods to the action overloads like this: 您可以为操作重载分配不同的HTTP方法,如下所示:

[HttpGet]
public async Task<ActionResult> Index()

[HttpPost]
public async Task<ActionResult> Index(int? mapCompanyId)

MVC runtime will then be able to pick a proper action based on the request HTTP method. 然后,MVC运行时将能够基于请求HTTP方法选择适当的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用控制器动作MVC5 - Calling a controller action MVC5 MVC异常:以下操作方法之间存在歧义 - MVC Exception: Ambiguous between the following action methods ASP.NET MVC模糊的动作方法 - ASP.NET MVC ambiguous action methods 在以下操作方法之间,当前对控制器类型“ ...”的操作请求[…]不明确 - The current request for action […] on controller type '…' is ambiguous between the following action methods ASP.NET MVC5 属性路由 - 只有一个匹配操作的模糊操作异常 - ASP.NET MVC5 Attribute routing - Ambiguous action exception with only one matching action 通过ASP.NET MVC5中的动态验证访问控制器和方法 - Access controller and methods by dynamic validation in asp.net Mvc5 MVC5控制器找不到使用“路线属性”的操作 - MVC5 Controller no action found using Route Attribute 无法使用MVC5使用控制器名称和操作名称访问WebAPI - Unable to access WebAPI using Controller and Action name using MVC5 模糊调用asp.net mvc动作方法 - ambiguous call asp.net mvc action methods 在以下操作方法之间,当前对控制器类型“ AdminController”的操作“ AdminManagement”的请求不明确 - The current request for action 'AdminManagement' on controller type 'AdminController' is ambiguous between the following action methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM