2016-05-12 23 views
5

當我在一個<a>標籤中使用asp-controller和asp-action替代當前的另一個Action時,在由Controller Method調用的視圖中使用[Route]屬性,生成的鏈接有一個空的href屬性。asp.net 5中tag-helpers和Route屬性之間的奇怪行爲MVC6

在控制器:

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> 

由於你可以看到,第一個鏈接沒有正確生成。所有以Currenct Action爲目標的另一個Action的鏈接都會生成一個空的href標記。

當我刪除了ShowSection行動[路線]屬性:

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

正如你所看到的,這些鏈接正確生成。

我該如何解決這個問題,同時保持我的[Route]屬性(或替代)?

+0

在你的'Startup.cs'中顯示你的'Index()'動作以及路由註冊 – haim770

回答

0

我終於找到了(@ haim770評論幫助):

我添加了一個路由屬性,以我的所有控制器&行動和現在的作品。