如何在控制器内获取@ Url.Action值

gre*_*eay 11 asp.net-core-mvc asp.net-core

我在用 ASP.net core

我可以在视图中使用Html操作

@Url.Action("GetOptions", "ControllerName", new { id="1"});
Run Code Online (Sandbox Code Playgroud)

但是我想在Controller中获取它的字符串值.

例如

string Url= Url.Action("GetOptions", "ControllerName", new { id="1"}).ToString();
Run Code Online (Sandbox Code Playgroud)

在以前版本的MVC中,您可以在控制器中引用辅助程序

  UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
Run Code Online (Sandbox Code Playgroud)

基本上我想要做的是在我的控制器中生成一个URL字符串表示

The*_*Man 8

为了使路由值能够正常工作,我不得不使用url helpers的静态调用

UrlHelperExtensions.Action(Url, "Details", "Resellers", new { id = 1 })

编辑:写这个的简写方式是:

this.Url.Action("Details", "Resellers", new { id = 1 })

谢谢@Learner.

  • 然后你可以刚刚调用`this.Url.Action("Details","Resellers",new {id = 1})` (4认同)