很抱歉问了这个基本问题。
从 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
}
最佳答案
您可以像这样简单地获取 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/