Code:
public ActionResult View(string id)
{
return View();
}
I currently get stackoverflow exceptions when I do this.
So if you are sure that your action method will return some view page, you can use ViewResult. But if your action method may have different behavior, like either render a view or perform a redirection. You can use the more general base class ActionResult as the return type.
What is an ActionResult? An ActionResult is a return type of a controller method, also called an action method, and serves as the base class for *Result classes. Action methods return models to views, file streams, redirect to other controllers, or whatever is necessary for the task at hand.
Inside the Views folder, we will add another folder for views that are associated with our HomeController and call that folder Home. Right-click on the Home folder and select Add → New Item. In the left pane, select the MVC View Page and enter index. cshtml in the name field and click on the Add button.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With