Reputation: 4001
I want to have a method returning ActionResult
, that has access to Response
/Json
and other things controllers have access to and use it from multiple controllers.
Any pointers?
Thanks
Upvotes: 4
Views: 2367
Reputation: 10166
You could consider making a base controller class and inheriting from that. This allows you to use the same method from multiple controllers.
I'm not sure of your experience level with MVC, but this article from Microsoft gives the basic foundations of ideas behind the controller and what you can do with it. And this question points to some well accepted examples of an MVC app.
Upvotes: 5
Reputation: 8393
Extending from what Nick has already pointed out here is a really silly example. Note, the HomeController inheriting from BaseController. The SomeResult action will be available within the HomeController.
For demonstration purposes only here is a ViewModel:
public class Customer
{
public string Name { get; set; }
public int Age { get; set; }
}
The Base Controller:
public class BaseController : Controller
{
public ActionResult SomeResult()
{
var customer = new Customer { Name = "Jon", Age = 15 };
return Json(customer, JsonRequestBehavior.AllowGet);
}
}
The Home Controller inheriting from the base controller:
public class HomeController : BaseController
{
public ActionResult Index()
{
return View("Index");
}
}
Upvotes: 2