C# MVC 4错误(如何更正应用程序中的服务器错误?)

C# MVC 4错误(如何更正应用程序中的服务器错误?),c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,说明:执行当前web请求期间发生未经处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息: System.Reflection.AmbiguousMatchException:的当前请求 控制器类型“CategoryController”上的操作“index”不明确 在以下操作方法之间:System.Web.Mvc.ActionResult Onclickmuseum.Controllers.CategoryController类型上的索引() System.W

说明:执行当前web请求期间发生未经处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:

System.Reflection.AmbiguousMatchException:的当前请求 控制器类型“CategoryController”上的操作“index”不明确 在以下操作方法之间:System.Web.Mvc.ActionResult Onclickmuseum.Controllers.CategoryController类型上的索引() System.Web.Mvc.ActionResult索引(Onclickmuseum.Models.CategoryModel) 在类型Onclickmuseum.Controllers.CategoryController上

源错误:

在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别

堆栈跟踪:

[AmbiguousMatchException:上的当前操作请求“索引” 控制器类型“CategoryController”在 以下操作方法:类型上的System.Web.Mvc.ActionResult Index() onclick.Controllers.CategoryController System.Web.Mvc.ActionResult索引(Onclickmuseum.Models.CategoryModel) 在类型Onclickmuseum.Controllers.CategoryController上]
System.Web.Mvc.Async.AsyncActionMethodSelector.FindAction(ControllerContext controllerContext,字符串actionName)+276
System.Web.Mvc.Async.ReflectedAsyncControllerDescriptor.FindAction(ControllerContext controllerContext,字符串actionName)+181
System.Web.Mvc.ControllerActionInvoker.FindAction(ControllerContext controllerContext,ControllerDescriptor ControllerDescriptor,字符串 actionName)+52
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext、字符串actionName、异步回调、对象 州)+295
System.Web.Mvc.c_displayClassId.b_17(异步回调) asyncCallback,对象asyncState)+83
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback 回调,对象状态,Int32超时)+161


您收到的错误表明ASP.NET MVC发现了两个同名的操作,无法选择要使用的操作。

这意味着MVC发现了两个同名的操作方法,并且混淆了。您可以通过以下方式消除歧义:

添加HTTP方法属性:

[HttpGet] // This method will be called only on GET http requests
public ActionResult Index() { ... }

[HttpPost] // This method will be called only on POST http requests
public ActionResult Index(int id) { ... }
// This method will be called for /ControllerName/Index requests
public ActionResult Index() { ... }

[ActionName("Index2")] // This method will be called for /ControllerName/Index2 requests
public ActionResult Index(int id) { ... }
指定操作名称:

[HttpGet] // This method will be called only on GET http requests
public ActionResult Index() { ... }

[HttpPost] // This method will be called only on POST http requests
public ActionResult Index(int id) { ... }
// This method will be called for /ControllerName/Index requests
public ActionResult Index() { ... }

[ActionName("Index2")] // This method will be called for /ControllerName/Index2 requests
public ActionResult Index(int id) { ... }
这可能是