-2

I would like to code two functions with same name and different parameters in controller as below.

[Route("Home/index")]
public JsonResult index()
{
    return new JsonResult {Data = 'test1'};
}

[Route("Home/index/{id}")]
public JsonResult index(id)
{
    return new JsonResult {Data = 'test2'};
}

But I got an the following error with this.

The current request for action 'index' on controller type 'HomeController' is ambiguous between the following action methods: System.Web.Mvc.JsonResult index() on type application.Controllers.HomeController System.Web.Mvc.JsonResult index(int) on type application.Controllers.HomeController

2 Answers2

0

I found the solution for this. The code is working but it need to insert following code in RouteConfig.cs.

routes.MapMvcAttributeRoutes();
0

How about something as you can define multiple routes

[Route("index")]
[Route("index/{id}")]
public JsonResult index(int id = null)
{

    if(id.HasValue()){
      return new JsonResult {Data = 'test2'};
    }

    return new JsonResult {Data = 'test1'};
}
cpoDesign
  • 8,953
  • 13
  • 62
  • 106