fah*_*mze 8 c# asp.net-mvc asp.net-core-mvc asp.net-core razor-pages
我有一个带有 Razor 页面的 ASP.NET Core 应用程序,我想利用 MVC 的优势。
\n我添加了
\nservices.AddMvc()启动我确实运行了该项目并测试了是否
\n但它不起作用并返回
\n\n\n“无法找到此本地主机页面\xe2\x80\x99”。
\n
您能帮我如何配置我的应用程序吗?
\nYiy*_*You 11
如果要将 MVC 添加到 Razor Page 项目:
首先需要在startup.cs中添加services.AddControllers();并ConfigureServices配置mvc路由:
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?}");
});
}
Run Code Online (Sandbox Code Playgroud)
其次,您需要添加两个具有这些名称的文件夹
ControllersViews。然后复制
Pages/Shared文件夹包含 _Layout.cshtml_ValidationScriptsPartial.cshtml到Views文件夹。
并复制
Pages/_ViewImports.cshtml到Views/_ViewImports.cshtml,Pages/_ViewStarts.cshtml 到Views/_ViewStarts.cshtml。这是一个项目结构:
| 归档时间: |
|
| 查看次数: |
4329 次 |
| 最近记录: |