Can You Overload Controller Methods in ASP.NET MVC

Can you overload controller methods in ASP.NET MVC?

You can use the attribute if you want your code to do overloading.

[ActionName("MyOverloadedName")]

But, you'll have to use a different action name for the same http method (as others have said). So it's just semantics at that point. Would you rather have the name in your code or your attribute?

Phil has an article related to this: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx

How to overload controller methods with same number of arguments in ASP.NET Core Web API?

You cannot do action overloads. The way routing works in ASP.NET Core is different than how it did in ASP.NET Web Api. However, you can simply combine these actions and then branch inside, since all params are optional:

public async Task<ActionResult<CustomerForWeb>> Get(int contactId, DateTime includeCustomersCreatedSince)
{
if (contactId != default)
{
...
}
else if (includedCustomersCreatedSince != default)
{
...
}
}

overload index action in Asp.net MVC

Just make one method:

 namespace global_vrf.Controllers
{
public class HomeController : Controller
{
public ActionResult Index(string language)
{
if(String.IsNullOrWhiteSpace(language))
{
string language="en-us";
}
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);

return View();
}
}
}

You can't make 2 methods becouse string could be null.

Overloading Controller Actions

You can't overload actions in this way as you have discovered.

The only way to have multiple actions with the same name is if they respond to different verbs.

I'd argue that having a single method which handles both situations is a cleaner solution and allows you to encapsulate your logic in one place and not rely on knowing about having multiple methods with the same name which are used for different purposes. - Of course this is subjective and just my opinion.

If you really wanted to have separate methods you could name them differently so that they clearly indicate their different purposes. e.g.:

public ActionResult GetAll() 
{
return PartialView(/*return all things*/);
}

public ActionResult Get(int id)
{
return PartialView(/*return 1 thing*/);
}

MVC ASP.NET Overloaded Controller Methods not working properly

The ChildActionOnly attribute ensures that an action method can be called only as a child method from within a view. so you no need to use [ChildActionOnly]. you are using a for go to the link so it's a GET request.

So remove

[HttpPost]
[ChildActionOnly]

And Change your return statement like:

return RedirectToAction("Pokemon", new {id = id});

Hopefully it's help for you.

Overloading in MVC

Till today you cannot overload your controller's Action method with same name but different parameters.

The only possibility is to have two overload, and for that you need to set the method's property to HttpGet and HttpPost. For example

[HttpGet]
public ActionResult foo(string myString)
{
return View();
}

[HttpPost]
public ActionResult foo(MyViewModelClass object)
{
return View();
}

And regarding your confusion,

From general convention, first method should be of type Get which gets called when someone sends request to access that page.

Second method is called when user submits a form with his login details.

Asp.Net MVC - Overload Action method

You need to decorate both overloads with an ActionMethodSelector attribute for disambiguation. ASP.NET MVC does not know how to select the appropriate overload.

A workaround is to handle both actions in the same method:

public ActionResult Index(int? id) {

if (id.HasValue) {
// id present
} else {
// id not present
}
}


Related Topics



Leave a reply



Submit