【发布时间】:2023-03-12 09:01:01
【问题描述】:
我正在尝试更改我的 ASP.NET Core MVC C# 应用程序中的起始页。我想先将用户带到一个登录页面,我已将Startup.cs 更改为:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Index}/{id?}");
}
}
我的控制器看起来像这样
public class LoginController : Controller
{
public IActionResult Index()
{
return View();
}
}
我有一个名为 Login.cshtml 的页面
我在这里错过了什么?
这是我的startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using eDrummond.Models;
namespace eDrummond
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<eDrummond_MVCContext>();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(opetions =>
{
options.LoginPath = "/Login/Index";
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseAuthentication();
app.UseCors(
options => options.AllowAnyOrigin()
);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Index}/{id?}");
});
}
}
}
我正在使用 VS2019 并创建了一个 asp.net 核心应用程序,然后选择了 MVC,我所做的只是 Scaffold Tables。所以配置应该是正确的?
【问题讨论】:
-
您的启动文件中有错字。你的代码真的可以编译吗? ` services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(opetions => { options.LoginPath = "/Login/Index"; });`
-
查看选项的拼写为
opetions。
标签: c# asp.net-core-mvc asp.net-core-3.1