【问题标题】:How to use urlHelper for the following with .net Core 2如何在 .net Core 2 中使用 urlHelper 进行以下操作
【发布时间】:2018-01-26 05:09:18
【问题描述】:

我的 mvc4 应用程序中有这个。

 <div class="text-center">
    @Html.ActionLink(Strings.ForgotPassword, "ForgotPassword", new { }, new { @class = "small", @id = "forgot" })
 </div>

我正在将此转换为支持 .net core 2.0。我尝试了以下方法,

@this.Url.Action("ForgotPassword", "Account", htmlAttributes: new { })

【问题讨论】:

  • 为什么对这个问题投反对票?我
  • 请查看How to Ask 了解如何提出一个好的问题。虽然您已经展示了您尝试过的方法,但您并没有明确说明这是否有效(我们只能猜测它对您不起作用),以及为什么它不起作用。此外,链接到操作是处理very early in the documentation 的一项非常基本的任务。您没有展示任何表明您知道自己实际尝试做什么的内容(例如,它是“ASP.NET Core”,而不是“.NET Core”,并且使用 mvc 4 对其进行标记也无济于事)。
  • 正如我刚才必须了解的那样,您甚至在 在此处发布问题之前就已经在 ASP.NET Core Slack 上讨论过这个问题,在那里您还收到了很多反馈您提供的信息根本无助于诊断您的问题。然而你甚至没有考虑过让你的问题更清楚。 – 我希望拥有 70k 声誉的人比这更了解如何提出一个好问题。
  • 当然!下次会这样做

标签: c# razor asp.net-core


【解决方案1】:

实际上,Url.Action 所做的是从动作和控制器生成 URL。 您要做的是生成一个html link or href element
所以在这里,你做错了。
你可以通过 -

@Html.ActionLink(Strings.ForgotPassword, "ForgotPassword", htmlAttributes: new { @class = "small", @id = "forgot" })

<a href="Url.Action("ForgotPassword", "Account")" class="small" id="forgot">@Strings.ForgotPassword</a>

两者都会产生相同的结果。

【讨论】:

    【解决方案2】:

    我不确定为什么 ActionLink 不适合您,但您也可以在 asp.net 核心应用程序中使用 asp-controllerasp-action,例如:

    <a asp-controller="Account" 
       asp-action="ForgotPassword" 
       class="small" 
       id="forgot">@Strings.ForgotPassword</a>
    

    更多详情请参考以下帖子:

    https://www.davepaquette.com/archive/2015/06/01/mvc-6-anchor-tag-helper.aspx

    希望对你有帮助!

    【讨论】:

    • 感谢您的回答,这可行,但 forgotpassword.cshtml 没有呈现它说 500
    • @Sajeetharan 500 表示您的控制器中可能出现了一些错误或异常,设置一个断点并检查它在哪里中断
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    相关资源
    最近更新 更多