Parse Query String at one place in ASP.NET MVC3

2019-09-06 16:55发布

问题:

I want to capture the query string code "?CODE=" which could be append to various links and this links will be used in the advertisements on other websites.

www.TestProject.com/Home?Code=ASCD

www.TestProject.com/FAQ?Code=ASCD2

www.TestProject.com/Tips?Code=ASCD3

So when the user clicks on this link the code will be captured and saved in a cookie. And if the user registers on the site then the code will be linked with his registration ID.

I have written the code in a static helper class to capture the URL. I make a call to this method from all the controller/Action Methods.

I wanted to know if this is the correct approach or should i call this method in Global.asax Application_BeginRequest() Method.

回答1:

I would create a custom ActionFilterAttribute.

You can use it to mark certain actions, controllers, or you can register it for all the actions in your website

protected void Application_Start()
{
    //...
    GlobalFilters.Filters.Add(new MvcProfilerGlobalAttribute());
}