0


This question is basically to know and justify whether what I am doing is a good practice or if there are any other ways to achieve the solution for the problem.
Here is the problem....

* I have an MVC application with many controllers and action methods. Customers will be accessing different action methods from different sources (google, bing, third party sites) adding a query string signature at the end.

* Since I dont want to check the querystring value in every action of the controller. I created a new controller "BaseController.cs" which inherits from the MVC Controller class. And implements the following code. All the application controllers will inherit from the BaseController.

    public class BaseController : Controller
    {
       public BaseController():base()
       { 
            string siteReference = HttpContext.Request["ref"];
       }
    }


I am requesting the experts of MVC to suggest better ways. Thanks in advance.

-KK

4

1 回答 1

3

There is a less obstrussive way to do it, you can use a global action filter to do the same without having to use a base controller, and you favor composition over inheritance.

  public class GetQueryString: IActionFilter, IMvcFilter
    {

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string siteReference = filterContext.HttpContext.Request["ref"];
        }


        public void OnActionExecuted(ActionExecutedContext filterContext)
        {

        }


        public bool AllowMultiple
        {
            get { return false; }
        }

        public int Order
        {
            get { return Filter.DefaultOrder; }
        }
    }
于 2012-08-13T17:39:42.610 回答