我對 .NET Core 還很陌生,曾經處理過較舊的 .NET 版本,例如 Windows 表單。我在我的_Layout頁面中設定了一個導航欄,我想用它來重定向到其他 Razor 頁面。
比如我有一個Home頁面(index.cshtml)一個檔案夾,名為在Home我的關閉Views檔案夾。如果我想從導航欄中選擇客戶搜索選項,我會使用一個呼叫以下函式的 onclick 事件。
function SearchClick() {
window.location.href = "CustomerSearchView.cshtml";
return false;
}
該CustomerSearchView是在CustomerSearch下面的檔案夾Home中的檔案夾中Views。還有一個CustomerSearchController帶代碼的
public IActionResult Index()
{
return View();
}
如果我嘗試將路徑添加到瀏覽器中,例如https://localhost:7022/CustomerSearchView.cshtml出現錯誤
無法找到此本地主機頁面 - 未找到該網址的網頁:https://localhost:7022/CustomerSearchView.cshtml
我已經使用完整路徑Views/CustomerSearch和其他人嘗試了各種變體,但得到了相同的錯誤。
我不熟悉路由程式,我不確定自己犯了什么錯誤,我認為這樣可以節省提問的時間,如果有人能指出來,我會很高興。
uj5u.com熱心網友回復:
試試這個
function SearchClick() {
window.location.href = "/CustomerSearch/Index";
return false;
}
uj5u.com熱心網友回復:
我通過更正路由并將以下代碼添加到我的 program.cs 中解決了該問題
app.MapControllerRoute(
name: "customersearch",
pattern: "{controller=CustomerSearchController}/{action=Index}/{id?}",
defaults: new { controller = "CustomerSearchController", action =
"Index" });
當添加任何帶有控制器的新視圖時,只需為其添加等效的路由代碼即可使其對專案可見。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/379760.html
