尝试从ASP.NET Core MVC Razor中的Layout页重定向时出错



我对.NET Core相当陌生,曾处理过Windows窗体等较旧的.NET版本。我在_Layout页面中设置了一个导航栏,我想用它重定向到其他Razor页面。

例如,我在Views文件夹的一个名为Home的文件夹中有一个Home页面(index.cshtml(。如果我想从导航栏中选择客户搜索选项,我会使用onclick事件,它会调用下面的函数。

function SearchClick() {
window.location.href = "CustomerSearchView.cshtml";
return false;
}

CustomerSearchView位于ViewsHome文件夹下方的CustomerSearch文件夹中。还有一个代码为的CustomerSearchController

public IActionResult Index()
{
return View();
}

如果我试图将路径添加到浏览器中,如https://localhost:7022/CustomerSearchView.cshtml,我会得到错误

找不到此localhost页面-找不到网址的网页:https://localhost:7022/CustomerSearchView.cshtml

我已经尝试过使用Views/CustomerSearch和其他路径的所有变体,但都得到了相同的错误。

我不熟悉路由程序,也不确定自己犯了什么错误,我认为这样可以节省询问的时间,如果有人能指出,我会非常高兴。

尝试这个

function SearchClick() {
window.location.href = "/CustomerSearch/Index";
return false;
}

我通过更正路由并将以下代码添加到我的程序中来解决问题。cs

app.MapControllerRoute(
name: "customersearch",
pattern: "{controller=CustomerSearchController}/{action=Index}/{id?}",
defaults: new { controller = "CustomerSearchController", action = 
"Index" });

当添加任何带有控制器的新视图时,只需为其添加等效的布线代码,即可使其对项目可见。

最新更新