我有一個帶有 Razor 頁面的 ASP.NET Core 應用程式,我想利用 MVC 的優勢,所以我將 Controllers 檔案夾添加到它的視圖中,并添加到它的services.AddMvc()啟動和另一個端點,然后嘗試運行(如果它有效)。只需運行該專案,我想測驗它是否回傳簡單視圖,并在默認地址后添加控制器名稱和操作,但它顯示“無法找到此本地主機頁面”。
你能幫我配置我的應用程式嗎?
uj5u.com熱心網友回復:
如果要將 MVC 添加到 Razor Page 專案:
1.You需要添加services.AddControllers();到ConfigureServices和配置MVC在startup.cs路由:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddControllers();
...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
2.您需要添加一個名為Controllers的檔案夾和一個名為的檔案夾Views。
然后將Pages/Shared包含 _Layout.cshtml和_ValidationScriptsPartial.cshtml的Views檔案夾復制到檔案夾。
并復制Pages/_ViewImports.cshtml 到Views/_ViewImports.cshtml、Pages/_ViewStarts.cshtml到Views/_ViewStarts.cshtml。
這是一個專案結構:

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/366913.html
標籤:C# asp.net-mvc asp.net核心 asp.net-core-mvc 剃刀页面
上一篇:為什么回應回傳204
