Controller Action Methods with different signatures
Posted
by Narsil
on Stack Overflow
See other posts from Stack Overflow
or by Narsil
Published on 2010-04-05T01:48:47Z
Indexed on
2010/04/11
22:33 UTC
Read the original article
Hit count: 2300
I am trying to get my URLs in files/id format. I am guessing I should have two Index methods in my controller, one with a parameter and one with not. But I get this error message in browser below.
Anyway here is my controller methods:
public ActionResult Index()
{
return Content("Index ");
}
//
// GET: /Files/5
public ActionResult Index(int id)
{
File file = fileRepository.GetFile(id);
if (file == null) return Content("Not Found");
else return Content(file.FileID.ToString());
}
Error:
Server Error in '/' Application.
The current request for action 'Index' on controller type 'FilesController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index() on type FileHosting.Controllers.FilesController System.Web.Mvc.ActionResult Index(Int32) on type FileHosting.Controllers.FilesController
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Reflection.AmbiguousMatchException: The current request for action 'Index' on controller type 'FilesController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index() on type FileHosting.Controllers.FilesController System.Web.Mvc.ActionResult Index(Int32) on type FileHosting.Controllers.FilesController
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[AmbiguousMatchException: The current request for action 'Index' on controller type 'FilesController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index() on type FileHosting.Controllers.FilesController
System.Web.Mvc.ActionResult Index(Int32) on type FileHosting.Controllers.FilesController]
System.Web.Mvc.ActionMethodSelector.FindActionMethod(ControllerContext controllerContext, String actionName) +396292
System.Web.Mvc.ReflectedControllerDescriptor.FindAction(ControllerContext controllerContext, String actionName) +62
System.Web.Mvc.ControllerActionInvoker.FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, String actionName) +13
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +99
System.Web.Mvc.Controller.ExecuteCore() +105
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
System.Web.Mvc.<>c_DisplayClass8.b_4() +34
System.Web.Mvc.Async.<>c_DisplayClass1.b_0() +21
System.Web.Mvc.Async.<>c__DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult
1.End() +59
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8677678
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Updated Code:
public ActionResult Index(int? id)
{
if (id.HasValue)
{
File file = fileRepository.GetFile(id.Value);
if (file == null) return Content("Not Found");
else return Content(file.FileID.ToString());
}
else
return Content("Index");
}
It's still not the thing I want. URLs have to be in files?id=3 format. I want files/3
routes from global.asax
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
//files/3
//it's the one I wrote
routes.MapRoute("Files", "{controller}/{id}",
new { controller = "Files", action = "Index", id = UrlParameter.Optional}
);
I tried adding a new route after reading Jeff's post but I can't get it working. It still works with files?id=2 though.
© Stack Overflow or respective owner