加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:49:05 所属栏目:asp.Net 来源:网络整理
导读:对不起基本问题. 从Global.asax中,我想获得控制器动作的绝对路径,就像我们从任何地方调用Response.Redirect(“?/ subfolder”)或通过调用@ Url.Content(“?/ controller / action” “)来自我们的观点. 在我的Global.asax事件中,我想做这样的事情: protecte
对不起基本问题.

从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
}

解决方法

这是 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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读