Right now I have just created an Initial Migration and connected to the SQL Server Management studio successfully. I had a default controller "Home" Index Page running successfully but once I created the Players controller and run the Index page it gives the error.
Please find the error mentioned below,
This localhost page can’t be found No webpage was found for the web address: https://localhost:44370/
Please find the Player.cs below
namespace Playersmanagementsystem.Models
{
public class Player
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Emailaddress { get; set; }
public int PhoneNumber { get; set; }
}
}
The players controller below
using Microsoft.EntityFrameworkCore;
using Playersmanagementsystem.Context;
using Playersmanagementsystem.Models;
namespace Playersmanagementsystem.Views
{
public class PlayersController : Controller
{
private readonly ApplicationDbContext _context;
public PlayersController(ApplicationDbContext context)
{
_context = context;
}
// GET: Players
public async Task<IActionResult> Index()
{
return View(await _context.Players.ToListAsync());
}
}
}
You can see in the Program.cs I have mentioned Players in the controller and Index in the action.
The Program.cs below
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Playersmanagementsystem.Context;
namespace Playersmanagementsystem
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/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();
app.MapControllerRoute(
name: "default",
pattern: "{controller= Players}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
}
}
}
The appsettings.json code below
{
"ConnectionStrings": {
"DefaultConnection": "Server=User;Database=Players;User Id=Ammadlogin;Password=yellowpass;Encrypt=True;TrustServerCertificate=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Please let me know the solution so that I can run the Player Index view page. I would like to clarify that the view for Players controller has been just created by Add-> new scaffolded items -> MVC controller with View using Entity Framework. thanks
This is because you have defined an incorrect route pattern. Try to remove the space character:
pattern: "{controller=Players}/{action=Index}/{id?}");