[英]localhost returns 404 after adding razor page to the existing web api (ASP.net 6 core)
我正在尝试将 razor 页添加到具有我的 web api 的项目中。 在我的Views
文件夹中,我添加了一个Receiving
文件夹,然后在其中添加了Index.cshtml
。 当我编译项目时,它会返回 404 错误,我不知道为什么。
程序.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();
索引.csthml
@{
ViewData["Title"] = "Receiving";
}
<h2>Shipping System</h2>
<p>Hello world</p>
有谁知道为什么它不显示Index.cshtml
? 先感谢您。
我加了
public IActionResult Index()
{
return View();
}
顺便说一句,在ShipmentController.cs
中
看起来你是 controller 路由需要一个 ID /{id?}
但你的 controller 操作没有id
参数。
您是否尝试明确浏览/Receiving/Index
? 根据您的路线配置,它将从Shipment
controller 返回 Index 视图。但我认为您没有创建Shipment
controller,而是使用Receiving
controller。因此,要么修改MapControllerRoute
代码以使用Receiving
controller。
app.MapControllerRoute(
name: "default",
pattern: "{controller=Receiving}/{action=Index}/{id?}");
或创建货件 controller 并查看索引。
检查视图的属性并确保“构建操作”不是“无”。 您可以将其设置为“内容”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.