asp.net - 从 Global.asax 中获取操作的绝对 URL 路径

标签 asp.net asp.net-mvc

很抱歉问了这个基本问题。

Global.asax 中,我想获取 Controller 操作的绝对路径,就像我们通过调用 Response.Redirect("~/subfolder") 获得的那样> 从任何地方或从我们的 View 中调用 @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
}

最佳答案

这是answer for your problem

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

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
}

关于asp.net - 从 Global.asax 中获取操作的绝对 URL 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16819585/

相关文章:

c# - 什么时候 User.Identity.IsAuthenticated 设置为 true

c# - 以其他用户身份登录时丢失 session /cookie

c# - await 运算符在 ASP.NET 上的行为与在 ASP.NET CORE 上的行为不同吗?

asp.net-mvc - ASP.NET MVC - 查看当前文件夹

jquery - 以某种方式在 aspx 站点上显示 JSON 结果

c# - 如何在 GridView 中编辑和更新行值?

asp.net - ASP Core Azure Active Directory - 获取名字和姓氏

mysql - 将泰米尔语文本存储到 MySQL 中

c# - 当参数发生异常时重定向

C# MVC 核心自定义 Bootstrap CSS