在ASP.NET 6 Core Web API中添加Razor Page后出现404错误求助
问题:ASP.NET 6 Web API项目添加Razor Pages后访问页面返回404
我在已有Web API的ASP.NET 6 Core项目中尝试添加Razor Pages,在Views文件夹下创建了Receiving文件夹,并在其中添加了Index.cshtml,但编译项目后访问该页面时返回404错误,不清楚原因。
相关代码
Program.cs
var builder = WebApplication.CreateBuilder(args); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true); builder.Services.AddControllersWithViews(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddHttpContextAccessor(); var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); builder.Services.AddDbContext<OperationsContext>(x => x.UseSqlServer(connectionString)); builder.Services.AddScoped<IContentRepository, ContentRepository>(); builder.Services.AddScoped<IPackageRepository, PackageRepository>(); builder.Services.AddScoped<IShipmentRepository, ShipmentRepository>(); builder.Services.AddScoped<IVendorRepository, VendorRepository>(); builder.Services.AddRazorPages(c => c.RootDirectory = "/Views"); //adds services for Razor Pages to the app. var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("v1/swagger.json", "MyAPI V1"); }); } if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/error"); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Shipment}/{action=Index}/{id?}"); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapRazorPages(); }); app.Run();
Index.cshtml
@{ ViewData["Title"] = "Receiving"; } <h2>Shipping System</h2> <p>Hello world</p>
ShipmentController.cs 已添加代码
public IActionResult Index() { return View(); }
原因分析
- 概念混淆:你创建的Index.cshtml是MVC视图,但缺少对应的
ReceivingController来处理请求;而Razor Pages需要特定的文件结构(通常带.cshtml.cs代码后置文件),路由基于文件路径,和MVC的控制器-动作模式完全不同。 - 路由不匹配:当访问
/Receiving/Index时,MVC会寻找ReceivingController的Index动作,但你只在ShipmentController中添加了Index方法,导致找不到对应路由;而你配置的Razor Pages因为缺少代码后置文件,也无法被框架正确识别为Razor页面。
解决方案
方案1:使用MVC视图(无需Razor Pages)
创建ReceivingController.cs,添加对应动作方法:
public class ReceivingController : Controller { public IActionResult Index() { return View(); } }
此时访问路径 /Receiving/Index 即可正常显示Views/Receiving/Index.cshtml。
方案2:使用Razor Pages
- 给Index.cshtml添加代码后置文件
Views/Receiving/Index.cshtml.cs:
using Microsoft.AspNetCore.Mvc.RazorPages; namespace YourProjectNamespace.Views.Receiving; public class IndexModel : PageModel { public void OnGet() { // 页面初始化逻辑(可选) } }
- 简化Program.cs中的路由配置(ASP.NET 6推荐极简写法):
// 替换原来的MapControllerRoute和UseEndpoints代码 app.MapControllers(); app.MapRazorPages();
- 访问路径
/Receiving即可打开该Razor页面(Razor Pages默认识别Index为页面首页,可省略文件名)。
内容的提问来源于stack exchange,提问作者smbbb
相关产品推荐
相关产品推荐

