2

I have a processrequest method in custom mvc handler.

I like to render specific action based on requestcontext.

following is my code

     public void ProcessRequest(HttpContext context)
    {
       if(Request.QueryString["lid"]!=null && ...){
        IController controller = new ListHell.Controllers.ErrorController();

        var routeData = new RouteData();
        routeData.Values.Add("controller", "Error");
        routeData.Values.Add("action", "Ops");

        var requestContext = new RequestContext(
             new HttpContextWrapper(Context), routeData);
        controller.Execute(requestContext);}
    }

In processrequest I am trying to call controller.execute method. But I am getting error that Context is not defined in current context.

Basically I am trying run specific action based on query string or cookie value before my request hit requested action. how can I do this? I believe I need to do the logic in mvcHandler. please correct me with right path I can follow?

Thanks

user786
  • 3,902
  • 4
  • 40
  • 72
  • Have you heard of `ActionFilters`? Basically **Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns.** - **[Source](https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs)** – Guruprasad J Rao Jun 12 '17 at 09:48
  • @GuruprasadRao How can I redirect to action in OnActionExecuting? – user786 Jun 12 '17 at 09:56
  • something like this - `filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary {{"controller", "Home"}, {"action", "Index"}});` – Guruprasad J Rao Jun 12 '17 at 09:57
  • @GuruprasadRao how can I use the actionfilter. how can I register it in the application so not to use default actionfilter? – user786 Jun 12 '17 at 10:04
  • You can either register it globally in `App_Start` under `Global.asax` or specific to controller in the controller level above controller name as `[ActionFilterName]` like how you use `[Authorize]` etc., – Guruprasad J Rao Jun 12 '17 at 10:05
  • @GuruprasadRao In browser its says redirect too many times and does not show the page – user786 Jun 12 '17 at 10:09
  • 1
    Set some global variable to identify the redirection. Once passed through this, make it invalid in actually method so that it will come again here.. – Guruprasad J Rao Jun 12 '17 at 10:42
  • 1
    @GuruprasadRao thanks – user786 Jun 12 '17 at 10:59
  • Great.. Happy Coding.. :) – Guruprasad J Rao Jun 12 '17 at 13:47

0 Answers0