【问题标题】:Strange behaviour between tag-helpers and Route attribute in asp.net 5 MVC6asp.net 5 MVC6 中标签助手和路由属性之间的奇怪行为
【发布时间】:2016-05-12 19:28:26
【问题描述】:

当我在 <a> 标记中将 asp-controller 和 asp-action 用于除当前之外的另一个 Action 时,在具有 [Route] 属性的控制器方法调用的视图中,生成的链接有一个空的 href属性。

在控制器中:

public class ForumController : Controller
{
    [Route("[action]/{sectionId:int}")]
    public async Task<IActionResult> ShowSection(int sectionId)
    {
        //some code
    }
}

在视图中:

<a asp-controller="Forum" asp-action="Index">Index</a>
<a asp-controller="Forum" asp-action="ShowSection" asp-route-sectionId="@Model.ParentSection.Id">@Model.ParentSection.Name</a>

生成的 html:

<a href="">Index</a>
<a href="/ShowSection/1">Général</a>

如您所见,第一个链接没有正确生成。所有指向当前 Action 之外的其他 Action 的链接都生成一个空的 href 标记。

当我删除 ShowSection 操作的 [Route] 属性时:

<a href="/Forum">Index</a>
<a href="/Forum/ShowSection?sectionId=1">Général</a>

如您所见,链接已正确生成。

如何在保留我的 [Route] 属性(或使用其他属性)的同时解决此问题?

【问题讨论】:

  • Startup.cs 中显示您的Index() 操作以及路由注册

标签: c# html asp.net-core-mvc tag-helpers asp.net5


【解决方案1】:

我终于找到了(@haim770 评论有帮助):

我为我的所有控制器和操作添加了一个 Route 属性,现在它可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 2016-01-18
    • 2018-04-05
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多