views:

69

answers:

3

Hi Experts,

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 http://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 http://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

A: 

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 });
Ufuk Hacıoğulları
Hi Narsil,Thanks for your answer. I tried with this method. but i cant call my method like this http://something/accounts/1it doesnt work that way. Yes it works if i write http://accounts/userinfo/1Thanks
Parminder
You can call your method directly from accounts/3 if you mapped the route correctly. Can you post your Global.asax?
Ufuk Hacıoğulları
Hi Narsil,I removed the default routing. Here is my global.asax routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute("UserInfo", //route name "Users/{id}/{name}", new { controller = "Users", action = "UserInfo", id = UrlParameter.Optional, name = UrlParameter.Optional });and here is controller method public ActionResult UserInfo(int? id, string name) { return View(); }It works nowthanks
Parminder
A: 

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.

Wikser
Hi Wikser,Thanks a lot for your answer. the default index works, but when I have to pass the parameters i have to say like http://something/accounts/index/1which i dont want to do. Regards
Parminder
+1  A: 

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.

Darin Dimitrov
Thanks Darin,I know this stuff. but requirment is different. regards
Parminder