【问题标题】:Get absolute URL path of an action from within Global.asax从 Global.asax 中获取操作的绝对 URL 路径
【发布时间】:2013-05-29 16:43:25
【问题描述】:

对不起,这个基本问题。

Global.asax 中,我想获得控制器操作的绝对路径,就像我们从任何地方调用Response.Redirect("~/subfolder") 或从我们的视图中调用@Url.Content("~/controller/action") 一样。

在我的 Global.asax 活动中,我想做这样的事情:

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
}

【问题讨论】:

  • 是否要比较 global.asax 中当前请求的控制器或操作名称?
  • 是的。我不知道如何从Global.asax 中到达RouteData,或者我可以使用RouteData.GetRequiredString("controller")

标签: asp.net asp.net-mvc


【解决方案1】:

如何检查会话超时

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

您有很多选择可以做到这一点。但是我不建议使用Global.asax的地方做这样的比较

选项 - 1

这也是很重要的方法。你可以使用HttpModule

选项 - 2

Base Controller class

选项 - 3

您可以将动作过滤器应用于整个控制器类,如下所示

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

          public ActionResult About()
          {

               return View();
          }
     }
}

每当调用 Home 控制器公开的任何操作时——无论是 Index() 方法还是 About() 方法,Action Filter 类都会首先执行。

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

如果你注意上面的代码,OnActionExecuting会在执行Action Method之前执行

选项 - 4

使用这种方法只会执行OnActionExecuting for Index 方法。

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

如何获取当前请求的DataTokens

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

【讨论】:

  • 谢谢,Pankaj。我知道这一切。但是,这些都没有帮助。我需要检查会话超时。最好的地方是 SessionStart 事件。
  • @watercoolerv2 - 不,您可以使用void Session_End(Object sender, EventArgs E) { 来检查会话结束。
  • Pankaj,拜托。如果您还没有阅读,我建议您阅读。请不要说明显的事情。会话结束时调用 Session_End。然后,对于生成的每个新会话,都会调用会话开始。因此,如果您想检测新生成的会话是否针对未经授权的用户,即会话已超时,您应该在 SessionStart 中而不是在 SessionEnd 中执行此操作。我建议你不要说明显的事情。
  • 很抱歉我被认为是个混蛋。我不是故意粗鲁的。感谢理解。
【解决方案2】:

这里是answer for your problem

您可以像这样简单地获取控制器和操作名称

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
}

【讨论】:

  • 我想知道,为什么不Base ControllerController 专门OnActionExecuting 进行此类比较?
  • @PankajGarg,基本上他想要这个在Global.asax
【解决方案3】:

最好创建一个ActionFilterAttribute 并像这样覆盖 OnActionExecuting 方法:

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

然后你可以在你的 BaseController 上应用属性,例如。

【讨论】:

  • 谢谢。我知道。我想在 Global.asax 中执行此操作,实际上是在 Session_Start 事件中检测会话超时。我不想为此创建操作过滤器或 HttpModule。
  • 好的,我建议这样做是因为您问题中的示例代码指向 Global.asax 的 Application_BeginRequest 方法。
  • 谢谢。我很感激。这个问题清楚地说明了意图。
猜你喜欢
  • 2012-08-27
  • 2015-11-15
  • 2016-07-23
  • 2012-10-22
  • 2021-10-18
  • 1970-01-01
  • 2011-02-07
  • 2012-10-25
相关资源
最近更新 更多