gpt4 book ai didi

c# - 从类库访问 ViewContext

转载 作者:太空狗 更新时间:2023-10-29 22:28:41 25 4
gpt4 key购买 nike

是否可以从类库访问ViewContext?我需要获取当前 View 和 Controller 操作的名称。我在我的类库项目中添加了 System.Web.dllSystem.Web.Mvc 但仍然无法找到获得所需内容的方法。尽管我可以使用 System.Web.HttpContext.Current 访问当前上下文。

最佳答案

您可以像这样访问当前路线信息:

var httpContext = new HttpContextWrapper(HttpContext.Current);
var routeData = System.Web.Routing.RouteTable.Routes.GetRouteData(httpContext);

var controllerName = routeData.Values["controller"].ToString();
var actionName = routeData.Values["action"].ToString();

关于c# - 从类库访问 ViewContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4563774/

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