在 ASP.NET Core 中运行控制器视图后找不到此 localhost 页面

问题描述 投票:0回答:1

现在我刚刚创建了初始迁移并成功连接到 SQL Server Management studio。我有一个默认控制器“主页”索引页面成功运行,但是一旦我创建了玩家控制器并运行索引页面,它就会出现错误。

请找出下面提到的错误,

找不到该本地主机页面 未找到该网址的网页:https://localhost:44370/

请找到下面的Player.cs

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; }

    }
}

下面的玩家控制器

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());
        }
     }
}

您可以在 Program.cs 中看到我提到了控制器中的玩家和动作中的索引。

下面的Program.cs

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();
        }
    }
}

下面的appsettings.json代码

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=User;Database=Players;User Id=Ammadlogin;Password=yellowpass;Encrypt=True;TrustServerCertificate=True;"

  },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

请让我知道解决方案,以便我可以运行玩家索引视图页面。 我想澄清一下,玩家控制器的视图是通过添加 -> 新的脚手架项目 -> MVC 控制器和使用实体框架的视图创建的。谢谢

c# asp.net-core asp.net-core-mvc
1个回答
0
投票

这是因为您定义了不正确的路线模式。尝试删除空格字符:

pattern: "{controller=Players}/{action=Index}/{id?}");
© www.soinside.com 2019 - 2024. All rights reserved.