5

Sorry for the basic question.

From within the Global.asax, I want to get the absolute path to a controller's action, like we get by calling Response.Redirect("~/subfolder") from anywhere or by calling @Url.Content("~/controller/action") from within our views.

In my Global.asax events, I'd like to do something like this:

protected void Application_BeginRequest(object sender, EventArgs args)
{
  if ( string.Compare(HttpContext.Current.Request.RawUrl, "~/foo", true) == 0 )
    // do something

    // I'd like the "~foo" to resolve to the virtual path relative to 
    // the application root
}
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336

3 Answers3

7

Here is the answer for your problem

You can simply get the controller and action name like this

protected void Application_BeginRequest(object sender, EventArgs args)
{
    HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);
    UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
    RouteData routeData = urlHelper.RouteCollection.GetRouteData(currentContext);
    string action = routeData.Values["action"] as string;
    string controller = routeData.Values["controller"] as string;

  if (string.Compare(controller, "foo", true) == 0)
    // do something

    // if the controller for current request if foo
}
Community
  • 1
  • 1
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
1

How to check the Session Timeout

void Session_Start(object sender, EventArgs e)
{
    if (Session.IsNewSession && Session["SessionExpire"] == null)
    {
        //Your code
    }
}

You have many options to do this. But I will not recommend to use Global.asax place to do such comparisons

Option - 1

This is also very important approach. You can use HttpModule.

Option - 2

Base Controller class

Option - 3

You can apply the Action Filter to an entire Controller class like below

namespace MvcApplication1.Controllers
{
     [MyActionFilter]
     public class HomeController : Controller
     {
          public ActionResult Index()
          {
               return View();
          }

          public ActionResult About()
          {

               return View();
          }
     }
}

Whenever any of the actions exposed by the Home controller are invoked – either the Index() method or the About() method, the Action Filter class will execute first.

namespace MvcApplication1.ActionFilters
{
     public class MyActionFilter : ActionFilterAttribute
     {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
                 //Your code for comparison
          }    
     }
}

If you pay attention to the above code, the OnActionExecuting will execute before executing the Action Method

Option - 4

Using this approach will execute the OnActionExecuting for Index method only.

namespace MvcApplication1.Controllers
{
     public class DataController : Controller
     {
          [MyActionFilter]
          public string Index()
          {
                 //Your code for comparison    
          }
     }
}

How to get the current request DataTokens

RouteData.Values["controller"] //to get the current Controller Name
RouteData.Values["action"]     //to get the current action Name
wwcdwdcw
  • 186
  • 1
  • 8
  • Thanks, Pankaj. I know all that. None of it helps, though. I need to check for session timeouts. The best place to do that is the SessionStart event. – Water Cooler v2 May 29 '13 at 17:22
  • @watercoolerv2 - Nope, you can use `void Session_End(Object sender, EventArgs E) {` to check the session end. – wwcdwdcw May 29 '13 at 17:26
  • Pankaj, please. If you haven't read up, I recommend that you do. Please don't state obvious things. Session_End is called when the session ends. Then, for every new session generated, session start is called. Hence, if you want to detect if the newly generated session is for an unauthorized user, that is, the session has timed out, you'd do that in the SessionStart and not in SessionEnd. I recommend you please not state obvious things. – Water Cooler v2 May 29 '13 at 17:32
  • I'm sorry I came across as a jerk. I didn't mean to be rude. Thanks for understanding. – Water Cooler v2 May 29 '13 at 19:32
0

It´s better to create an ActionFilterAttribute and override OnActionExecuting method like this:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (filterContext.ActionDescriptor.ActionName == "Foo") 
    {
        // do something
    }
    base.OnActionExecuting(filterContext);
}

Then you can apply the attribute on your BaseController, for example.

Daniel Silva
  • 379
  • 1
  • 11
  • Thanks. I know that. I want to do it in the Global.asax, actually in the Session_Start event to detect session timeouts. I don't want to create an action filter or HttpModule for this. – Water Cooler v2 May 29 '13 at 17:12
  • Ok, I suggested this because the sample code in your question points to Application_BeginRequest method of Global.asax. – Daniel Silva May 29 '13 at 17:18
  • Thanks. I appreciate that. The question clearly states the intention. – Water Cooler v2 May 29 '13 at 17:23