[英]Html.ActionLink with an area and parameters and html attribures
[英]Why is this ActionLink to another area not working?
我正在測試如何構建指向另一個區域的頁面的操作鏈接,並且我找到的每個來源都說要使用
Html.ActionLink("[Link Text]", "[action name]", "[controller]", new { area = "[areaName]" }, null)
除了我發現的一個來源建議
Html.ActionLink("[Link Text]", "[action name]",new { area = "[areaName]", controller = "[controllerName]" })
問題是,這些都不適合我。 在我的 MVC 應用程序中,我有一個名為“Uploader”的區域,其中包含自己的 Home 控制器和 Index 頁面。 因此,在我的 MVC 項目的主索引頁面中,我創建了一個如下所示的 ActionLink:
@Html.ActionLink("Area Test", "Index", "Home", new { area = "Uploader" }, null)
如果鏈接正常,我將被帶到一個頁面,上面寫着“這是上傳者”,我的區域索引頁面上的文本,但相反,我的應用程序的主主頁/索引頁面只是重新加載。
重新加載后我在瀏覽器中看到的地址是“https://localhost:44352/?area=Uploader”。
該區域的 HomeController 使用“Area”標簽正確格式化,如下所示:
namespace TestProject.Areas.Uploader.Controllers
{
[Area("Uploader")]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
我正在使用.Net 5。我做錯了什么?
終於在這里找到了答案。 事實證明,我的區域配置倒退了:“默認”路線應該排在最后。
它應該是:
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
...而不是相反。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.