开发者

Best approach to don't request same info over and over

开发者 https://www.devze.com 2023-03-04 16:01 出处:网络
On my controller I have it inherit a MainController and there I override the Initialize and the OnActionExecuting.

On my controller I have it inherit a MainController and there I override the Initialize and the OnActionExecuting.

Here I see what is the URL and by that I can check what Client is it, but I learned that for every Method called, this is fired up again and again, even a simple redirectToAction will fire the Initialization of the same controller.

Is there a better technique to avoid this repetition of database call? I'm using Entity Framework, so it will take no time to call the DB as it has the result in cache already, but ... just to know if there is a better technique now in MVC3 rather that host the variables in a Session Variable

sample code

public class MyController : MainController
{
    public ActionResult Index()
    {
        return View();
    }
}


public class MainController : Controller
{

   public OS_Clients currentClient { get; set; }

   protected override void Initialize(System.Web.Routing.RequestContext requestContext)
   {
        // get URL Info
        string url = requestContext.HttpContext.Request.Url.AbsoluteUri;
        string action = requestContext.RouteData.GetRequiredString("action");
        string controller = requestContext.RouteData.Get开发者_开发问答RequiredString("controller");
        object _clientUrl = requestContext.RouteData.Values["cliurl"];

        if (_clientUrl != null && _clientUrl.ToString() != "none")
        {
            // Fill up variables
            this.currrentClient = db.FindClientById(_clientUrl.ToString());
       }

        base.Initialize(requestContext);
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // based on client and other variables, redirect to Disable or Login Actions
        // ... more code here like:

        //  filterContext.Result = RedirectToAction("Login", "My");

        base.OnActionExecuting(filterContext);
    }
}

is it still best to do as:

public OS_Clients currentClient { 

    get {
        OS_Clients _currentClient = null;

        if (Session["CurrentClient"] != null)
            _currentClient = (OS_Clients)Session["CurrentClient"];

        return _currentClient;
    }

    set { 
        Session["CurrentClient"] = value; 
    }
}


It seems that you dealing with application security in that case I would suggest to create Authorization filter, which comes much early into the action. You can put your permission checking code over there and the framework will automatically redirect the user to login page if the permission does not meet AuthorizeCore.

Next, if the user has permission you can use the HttpContext.Items as a request level cache. And then you can create another ActionFilter and in action executing or you can use the base controller to get the user from the Httpcontext.items and assign it to controller property.

If you are using asp.net mvc 3 then you can use the GlobalFilters to register the above mentioned filters instead of decorating each controller.

Hope that helps.


In your base controller, you need to cache the result of the first call in a Session variable.

This makes sure the back-end (DB) is not called unnecessarily, and that the data is bound to the user's Session instead of shared across users, as would be the case with the Application Cache.

0

精彩评论

暂无评论...
验证码 换一张
取 消