Get absolute action url from Global.asax

Sorry for the basic question.

From Global.asax I want to get the absolute path to the controller action, for example, we call Response.Redirect("~/subfolder") from anywhere or call @Url.Content("~/controller/action") from our views.

In my Global.asax events, I would 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 } 
+4
source share
3 answers

Here is the answer for your problem

You can just get the controller name and actions 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 } 
+6
source

It is better to create an ActionFilterAttribute and override the OnActionExecuting method as follows:

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

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

0
source

How to check session timeout

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

You have many options for this. But I will not recommend using Global.asax place for such comparisons.

Option 1

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

Option - 2

 Base Controller class 

Option - 3

You can apply an action filter to the entire controller class as shown below

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

Whenever you invoke an action opened by the Home controller, either the Index () method or the About () method, the Action Filter class is first run.

 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, OnActionExecuting will execute before executing the action method

Option - 4

With this approach, only the OnActionExecuting index-only method will be executed.

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

How to get the current DataTokens request

 RouteData.Values["controller"] //to get the current Controller Name RouteData.Values["action"] //to get the current action Name 
0
source

Source: https://habr.com/ru/post/1483414/


All Articles