使用anchor tag helper,我們如何在新的瀏覽器窗口選項卡中打開ASP.NET Core MVC View
。我嘗試了以下,但首先它抱怨target
屬性也需要具有href
屬性。但是,正如我們所知,我們不能將asp-action
屬性中的href屬性用於MVC Core
;否則我們會得到如下所示的錯誤。 注意:我已經看到了諸如t his one一些建議,但他們沒有標籤相關幫手:錨標籤助手打開視圖在新窗口中 - 選項卡
<a asp-action="testAction" href="#" target="_blank">Click Here</a>
錯誤:
InvalidOperationException: Cannot override the 'href' attribute for . An with a specified 'href' must not have attributes starting with 'asp-route-' or an 'asp-action', 'asp-controller', 'asp-area', 'asp-route', 'asp-protocol', 'asp-host', or 'asp-fragment' attribute.
如果忽略關於目標屬性的visual studio警告,它仍然有效。 您可以刪除href並保持目標。還關閉錨定標記。 否則您可以使用Click Here 另一個有趣的解決方案是添加您的自定義標籤助手。有關更多信息,請參閱此答案https://forums.asp.net/t/2083444.aspx。 –
請注意,沒有MVC6了,因爲它被重新命名以避免混淆。如果你**保留使用它,你只會增加其他人的困惑。 – Tseng
@MohamedRozza你在這個特殊情況下忽略VS警告的建議奏效。謝謝。 – nam