gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:36:26 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com