MVC3 is a helper method that will be used by several controllers

I want to have a method that returns an ActionResult that has access to Response / Json , and other controllers have access to it and use it from multiple controllers.

Any pointers?

thanks

+4
source share
2 answers

You might consider creating a controller base class and inheriting from it. This allows you to use the same method from multiple controllers.

I'm not sure about your experience with MVC, but this article from Microsoft gives the basic basics of the ideas behind the controller and what you can do about it. And this question points to some well-accepted examples of an MVC application.

+5
source

Extending from what Nick has already indicated here is a really stupid example. Note that HomeController inherits from BaseController. SomeResult action will be available in HomeController.

For demo purposes, only here ViewModel:

 public class Customer { public string Name { get; set; } public int Age { get; set; } } 

Base controller:

 public class BaseController : Controller { public ActionResult SomeResult() { var customer = new Customer { Name = "Jon", Age = 15 }; return Json(customer, JsonRequestBehavior.AllowGet); } } 

Home controller inheriting from the base controller:

 public class HomeController : BaseController { public ActionResult Index() { return View("Index"); } } 
+2
source

Source: https://habr.com/ru/post/1382574/


All Articles