There is an ASP.NET MVC 5 application. There are a number of controllers. So, I need to make a series of repetitive actions for each controller. How?

I am sure that they will ask for specifics (although I am not sure that this is necessary), so I’ll tell you about the actions.

If the user requested the following URL: http://my.app/Controller/Page/?qwe=asd , then certain variables (not all, but in this case, qwe exactly) should be written in cookies, and then the redirect should go to the URL: http://my.app/Controller/Page

How to do it?

    2 answers 2

    Options:

    1. HttpApplication.BeginRequest 

    In Global.asax.cs get a method

     protected void Application_BeginRequest(object sender, EventArgs e) { } 

    which will be called by ASP.Net at the beginning of processing each request.

     2. ActionFilterAttribute.OnActionExecuting 

    Write the heir ActionFilterAttribute , in it you rewrite the virtual method OnActionExecuting , where when you need to assign filterContext.Result . Decorate your controllers with this attribute.

     3. Controller.OnActionExecuting 

    Get a base class for your controllers; in its rewritten OnActionExecuting method, OnActionExecuting analyze the need to redirect the current request.

    • And you can read more? With Controller.OnActionExecuting everything is clear, but with the other two ... In what place should they be used? - iRumba
    • @IRumba Read more about the second option here: metanit.com/sharp/mvc5/8.1.php - AK

    You can write a module, I think they are intended for just such cases

     public class RedirectModule : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += HandleRequest; } public void HandleRequest(object sender, EventArgs e) { var req = HttpContext.Current.Request; var existingParams = new List<string> { "qwe" //Ну и остальные параметры }; foreach(var param in existingParams) { if(req.QueryString[param] != null) { var cookies = new HttpCookie("MyCookies"); cookies[param] = req.QueryString[param]; HttpContext.Current.Response.Redirect("нужный редирект"); } } } } 
    • and what to do next with the module with this? - iRumba
    • You can find @iRumba in web.config system.webServer-> modules, add the name and type of the module to it, google it anyway - Buka