Home > OS >  localhost returns 404 after adding razor page to the existing web api (ASP.net 6 core)
localhost returns 404 after adding razor page to the existing web api (ASP.net 6 core)

Time:09-28

I am trying to add razor pages to the project that has my web api's. In my Views folder, I have added a Receiving folder, then Index.cshtml in it. When I compiled the project, it would return 404 error and I have no idea why.

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.csthml

@{
    ViewData["Title"] = "Receiving";
}

<h2>Shipping System</h2>
<p>Hello world</p>

Does anyone know why is it not displaying Index.cshtml? Thank you in advance.

I added

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

in ShipmentController.cs by the way

CodePudding user response:

Looks like you're controller route expects an ID /{id?} but your controller action does not have an id param.

CodePudding user response:

Did you try browsing the /Receiving/Index explicitly? Based on your route configuration, it will return the Index view from the Shipment controller. But I think you've not created the Shipment controller instead you're using a Receiving controller. So either modify the MapControllerRoute code to use the Receiving controller.

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Receiving}/{action=Index}/{id?}");

or create a shipment controller and view for the Index.

  • Related