Home > Back-end >  Detect QueryString and create Session on any Request URL
Detect QueryString and create Session on any Request URL

Time:10-22

Currently, I'm detecting a QueryString and creating the session on the Action of the Controller. The query string can be coming in with the url to any deep page within the site. I don't want to code it in most of my Actions.

Therefore, I'm wondering, is there a generic way to detect QueryString in the URL in any request that comes to site's pages. I think I can create a 'BaseController' from the 'Controller' and change all my current Controller's to inherit from BaseController.

Is there any other ways? may be in RouteConfig.cs? Startup.Auth.cs?

CodePudding user response:

(.Net Core) You can craete custom middleware to detect any request using Httpcontext. read more about middleware . example of custom middleware

CodePudding user response:

Thanks KHAL, for your hint.

so in classic ASP.NET MVC, in Startup.Auth.cs file

app.Use(async (context, next) =>
        {
            var referral = context.Request.Query["referral"];
            if(referral == "whatever")
                {
                  //Do your thing ;)
                }

            // Call the next delegate/middleware in the pipeline
            await next();
        });

However, note that the position of this inside the Startup.Auth.cs file is critical, if you want to use Session. It cannot be at the top of the file

  • Related