Perhaps a stupid question, but I am a beginner as a writer, and most likely the answer to it is yes, but google did not help me, because the question is rather abstract and you can’t get away with a single query.

Is it possible to create a special controller whose methods have no representations, but simply perform functions such as working with cookies, sessions, and so on. Because if you create a class in Models, you will not be able to perform request and response cookies there (for example, to check the user's authorization), because he is not associated with the client and works only from the controller. And to write in each controller a method that checks authorization, or, for example, destroys a session, is too wrong, as, again, to use a class in Models for which you need to transfer a cookie created in the controller each time, and then return it back and execute the rest unique for each case of action. It is much easier to use an intermediary controller that performs everything in itself.

It may all work with controllers for a long time, and I use one for this purpose in Models, where, for example, to change the value of cookies, I write in the controller:

Response.Cookies.Add(new CookiesManager().Change(Request.Cookies["s"])); 

CookiesManager model:

 public HttpCookie Change(HttpCookie cookie) { string name=cookie.Name; ... //какой-то там код определяющий новое значение на основе старого return new HttpCookie(name,value);//value - новое значение } 

    1 answer 1

    1) It is possible to create a controller whose methods have no representations, but simply perform certain functions. Just remember that each action method of the controller must return an ActionResult.

    2) However, for repeating operations and checks in the ASP.NET MVC infrastructure, a special attribute ActionFilterAttribute is provided. This is an abstract class that contains methods that will be executed BEFORE and AFTER the action of the controller, as well as BEFORE and AFTER the result of the action has been executed. In your own implementation of this attribute, you can use any of these actions for the desired purposes.

    3) To check the authorization, there is already a special attribute - AuthorizeAttribute .

    The attributes listed above can be hung both on the individual action methods of the controller, and on the entire controller at once.