简体   繁体   中英

Can I have an ActionResult called “View”

Code:

public ActionResult View(string id)
{ 
   return View();
}

I currently get stackoverflow exceptions when I do this.

You should be getting a compiler warning that your definition of View masks that of the base controller class and that you should explicitly use the new keyword. If you change your code to do this instead, it should work as you'd like:

return base.View();

Of course, just don't call yourself recursively:

public new ActionResult View()
{
    return base.View();
}

It's generally a good idea to name your views descriptively. A view named View doesn't say what the view does or the data it's likely to use. I would highly suggest giving it a better name.

That said, in this instance, you're recursively calling yourself, so change the return statement to

return base.View();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM