user788448
user788448

Reputation: 797

Razor View Does Not Display Records

I am new to ASP.NET Core MVC. I created an ASP.NET Core MVC project in VS 2022. I used EF power tool to create DbContext and model classes, added connection string route map in program.cs.

But my view is blank and does not display any records from controller. Actually, the HomeController never gets hit when debugging. I have no idea where the problem is and what code I am missing.

Program.cs:

using Courses.Models;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews();
//add connection string
builder.Services.AddDbContext<DbContext>(options =>
{
    
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();
//map route
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.MapRazorPages();

app.Run();

HomeController

using Microsoft.AspNetCore.Mvc;
using Courses.Models;

namespace Courses.Controllers
{
    public class HomeController : Controller
    {
        private readonly DbContext _db;

        // GET: HomeController
        public HomeController(DbContext context)
        {
            _db = context;
        }
    
        public ActionResult Index()
        {
            var subjectList = _db.subjectTable.OrderBy(a => a.Subject).ToList();
            return View(subjectList);
        }

        ...
    }
}

Upvotes: 0

Views: 56

Answers (1)

user788448
user788448

Reputation: 797

Never mind. When I created the project, it generates a Pages folder with pages underneath it. My HomeController gets hit after I removed the Pages folder.

Upvotes: 0

Related Questions