Parminder
Parminder

Reputation: 3158

overriding Method asp.net mvc

Can I override an ActionResult method. Say I have a method Index in AccountController like this

public ActionResult Index() {

return View(); }

Can I have one more method with same name but with differnt parameters like public ActionResult Index(int userid) {

return View(); }

I dont want to call it like http://something/accounts/index/11 I just want to say http://something/accounts/11

You can look at stackoverflow stuff also if you go to https://stackoverflow.com/users i feel users is the controller and the default action is index so dont to call it explicit. now if you type something like https://stackoverflow.com/users/96346/parminder the two parameters are 96346 and parminder

I hope it makes sense.

what will be the entries in the global.asax

Regards Parminder

Upvotes: 1

Views: 760

Answers (3)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

Take a look at my question here

I think you can do it by hacking some method attributes but I think it would be cleaner if you write another method then add a route for it.It would look like this:

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

public ActionResult UserInfo(int id)
{
    //some stuff
    return View(modelObject);
}

Then you provide a route for it like:

routes.MapRoute("UserInfo", //route name
                "Accounts/{id}",
                new 
                { 
                    controller = "Accounts",
                    action = "UserInfo",
                    id=UrlParameter.Optional 
                });

Upvotes: 0

Wikser
Wikser

Reputation: 820

You want to over-load the method. But you don't need to.

Just make the parameter nullable:

public ActionResult Index(int? userid)
{
   return (userid.HasValue) ? ShowUser(userid.Value) : ShowOverview();
}

You can take the default route map for this example.

For your (changed) requirements you'll indeed need to modify your routes using constraints.

routes.MapRoute("directaccount", "Accounts/{userid}/{someotherparam}",
    new { controller="Accounts", action="ShowAccountByID", someotherparam=null }
    new { userid=@"\d+"});

*here goes the rest*

The reg-ex ensures that your other calls won't get eaten by this route. This is untested. I don't recommend to use overloading for this functionality.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You can do action overloading (two actions with same name on the same controller with different arguments) but they should be invocable on different HTTP verbs. This is a commonly pattern in ASP.NET MVC applications: one action accessible through GET that renders a view containing a form and a second action that this form will POST to:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost] // [AcceptVerbs(HttpVerbs.Post)] for ASP.NET MVC 1.0
    public ActionResult Index(SomeViewModel model)
    {
        if (ModelState.IsValid)
        {
            // TODO: validation passed => do something with the model
        }
        return RedirectToAction("index");
    }
}

There's no need to modify your routes for this, it works with the default ones.

Upvotes: 1

Related Questions