我正在使用以下代码来获取我的视图中的当前“动作”,因为我想根据自定义构建一个actionlink。
ViewContext.RequestContext.RouteData.Values("action")
我的最终目标是使用Javascript构建一些动作链接,而.js需要知道当前的控制器和操作是什么,因为我希望它是灵活的。我通过浏览框架发现了以上内容,但我不知道我是否找到正确的东西。
即
var routeData = ViewContext.RequestContext.RouteData; var linkStub = '/@routeData.Values("controller")/@routeData.Values("action")';
有人知道这是否是最简单/最简单的方法呢?
解决方法
最干净的方式将是一种扩展方法
public static class MyUrlHelper { public static string CurrentAction(this UrlHelper urlHelper) { var routeValueDictionary = urlHelper.RequestContext.RouteData.Values; // in case using virtual dirctory var rootUrl = urlHelper.Content("~/"); return string.Format("{0}{1}/{2}/",rootUrl,routeValueDictionary["controller"],routeValueDictionary["action"]); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。