Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is overloading in ASP.NET MVC5 not possible?

Tags:

asp.net

i have a Action called Index in a controller. This can be called like the following:

  1. Without parameter
  2. With a custon enum
  3. With a datetime

I have a custom attribute which should be set to 2. and 3., but not to 1. So I tried the following:

    public ActionResult Index() {
        return View();
    }

    [MyCustomAttribute]
    public ActionResult Index(StatusEnum status) {
        return View();
    }

    [MyCustomAttribute]
    public ActionResult Index(DateTime date) {
        return View();
    }

That seems not possible, I always get an error that Index is ambiguous. But I can't really understand why: The Actions have different parameters, so it should be no problem to distinguish them by the parameters.

When I try something like this, I get the same error:

    public ActionResult Index() {
        return View();
    }

    [MyCustomAttribute]
    public ActionResult Index(StatusEnum status, DateTime date) {
        return View();
    }

In this case it should be REALLY clear and no problem to handle all requests without parameters to the first Action and the rest to the second one. I think this is very strange, because in native C# its definitely no problem to make such overloads.

The second point: When overloading is for whatever reason not possible, how can I handle that? Excluding the Index-Action without parameters (when possible) is no option because I use the attribute in other controllers too, and there should be all Index-Actions handled.

like image 805
Lion Avatar asked Jan 12 '23 03:01

Lion


2 Answers

While overloading makes sense from a C# perspective, the MVC routing doesn't know how to deal with some overloads. You can get your overloads to work if you used typed parameters in Attribute Routing...

[Route("controller/action")]
public ActionResult Index() {
    return View();
}

[MyCustomAttribute]
[Route("controller/action/{status:int}")]
public ActionResult Index(StatusEnum status) {
    return View();
}

// 
[MyCustomAttribute]
[Route("controller/action/{date:datetime}")]
public ActionResult Index(DateTime date) {
    return View();
}

Add this line to RouteConfig.cs to enable Attribute Routing...

routes.MapMvcAttributeRoutes();
like image 69
Anthony Chu Avatar answered Jan 13 '23 17:01

Anthony Chu


When I have this URL:

http://www.test.com/

Where do you think the route will take you? It can go in the Index() action obviously but why shouldn't it be able to go to the Index(StatusEnum status, DateTime date) action? Both parameters are nullable so they will just be null.

The point is that the routing engine won't know what you want, maybe you want to have null parameters, maybe not.

You can try something like this:

public ActionResult Index() {
    return View();
}

[Route("SetStatus/{status}")]
public ActionResult Index(StatusEnum status) {
    return View();
}

[Route("SetDate/{date}")]
public ActionResult Index(DateTime date) {
    return View();
}
like image 30
Roger Far Avatar answered Jan 13 '23 17:01

Roger Far