C# 更改Asp.Net核心应用程序中的默认页面
我正在尝试更改ASP.NET核心MVC应用程序的起始页。我想首先让用户进入登录页面,我在C# 更改Asp.Net核心应用程序中的默认页面,c#,asp.net-core-mvc,asp.net-core-3.1,C#,Asp.net Core Mvc,Asp.net Core 3.1,我正在尝试更改ASP.NET核心MVC应用程序的起始页。我想首先让用户进入登录页面,我在Startup.cs中更改为: public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", p
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?}");
});
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.HttpsPolicy;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
使用eDrummond.模型;
名称空间eDrummond
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
public void配置服务(IServiceCollection服务)
{
services.AddControllersWithViews();
services.AddDbContext();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(操作=>
{
options.LoginPath=“/Login/Index”;
});
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
app.UseAuthentication();
app.UseCors(
options=>options.AllowAnyOrigin()
);
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{controller=Login}/{action=Index}/{id?}”);
});
}
}
}
我正在使用VS2019,创建了一个asp.net核心应用程序,然后选择了MVC,我所做的就是搭建表格。所以配置应该正确吗?您需要考虑身份验证流。首先,你是未经授权的。当您访问任何未经授权的页面时,您希望重定向到您的登录页面,对吗?然后你需要告诉这个程序:
public void ConfigureServices(IServiceCollection services) {
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
// What kind of authentication you use? Here I just assume cookie authentication.
.AddCookie(options =>
{
options.LoginPath = "/Login/Index";
});
}
public void Configure(IApplicationBuilder app) {
// Add it but BEFORE app.UseEndpoints(..);
app.UseAuthentication();
}
下面是一个stackoverflow主题,它解决了您的问题:
编辑:
/Pages
Index.cshtml
Index.cshtml.cs
/Login
Index.cshtml
Index.cshtml.cs
/Startup.cs
事实证明,你可以这样做:
2。编辑:
/Pages
Index.cshtml
Index.cshtml.cs
/Login
Index.cshtml
Index.cshtml.cs
/Startup.cs
因此,我的第一个答案没有错,但它没有包括它需要对控制器进行的更改。
有两种解决方案:
/Pages
Index.cshtml
Index.cshtml.cs
/Login
Index.cshtml
Index.cshtml.cs
/Startup.cs
文件:
/Pages
Index.cshtml
Index.cshtml.cs
/Login
Index.cshtml
Index.cshtml.cs
/Startup.cs
//文件位于/Startup.cs中:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.Configuration;
namespace stackoverflow_aspnetcore_59448960
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Does automatically collect all routes.
services.AddRazorPages();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
// That will point to /Pages/Login.cshtml
options.LoginPath = "/Login/Index";
}); ;
services.AddAuthorization(options =>
{
// This says, that all pages need AUTHORIZATION. But when a controller,
// for example the login controller in Login.cshtml.cs, is tagged with
// [AllowAnonymous] then it is not in need of AUTHORIZATION. :)
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
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();
app.UseEndpoints(endpoints =>
{
// Defines default route behaviour.
endpoints.MapRazorPages();
});
}
}
}
//文件位于/Pages/Login/Index.cshtml.cs中:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace stackoverflow_aspnetcore_59448960.Pages.Login
{
// Very important
[AllowAnonymous]
// Another fact: The name of this Model, I mean "Index" need to be
// the same as the filename without extensions: Index[Model] == Index[.cshtml.cs]
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
}
}
使用Microsoft.AspNetCore.Authorization;
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Microsoft.Extensions.Logging;
命名空间stackoverflow_aspnetcore_59448960.Pages.Login
{
//非常重要
[异名]
//另一个事实:这个模型的名称,我的意思是“索引”需要
//与不带扩展名的文件名相同:Index[Model]==Index[.cshtml.cs]
公共类索引模型:PageModel
{
专用只读ILogger\u记录器;
公共索引模型(ILogger记录器)
{
_记录器=记录器;
}
公共互联网
{
}
}
}
//文件位于/Pages/Index.cshtml.cs中
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace stackoverflow_aspnetcore_59448960.Pages
{
// No [Authorize] needed, because of FallbackPolicy (see Startup.cs)
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
}
}
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Microsoft.Extensions.Logging;
命名空间stackoverflow_aspnetcore_59448960.Pages
{
//由于回退策略,不需要[Authorize](请参阅Startup.cs)
公共类索引模型:PageModel
{
专用只读ILogger\u记录器;
公共索引模型(ILogger记录器)
{
_记录器=记录器;
}
公共互联网
{
}
}
}
您应该具有如下所示的文件夹结构
Views
Login
Index.cshtml
默认情况下,您应该可以登录页面。我会根据您的帖子添加代码,但当我启动我的应用程序时,它仍然会将我带到index.cshtml页面。我使用的是.NET Core 3.1你能在这里添加你的登录控制器的代码吗?哦,我想。。。您没有使用cshtml文件吗?这些文件称为Razor页面。当我从Visual Studio 2019启动我的应用程序时,我被带到localhost:44336,它显示\Views\Home\Index.cshtml-仍然没有显示我的\Views\Login\Index.cshtmloh,我认为Razor页面有所不同。我正在使用.cshtml页面。我的错误。啊-也许这是我的问题。我的文件夹结构如下\Views\Home\index.cshtml\视图\Home\Login.cshtml\Views\Home\Privacy.cshtmlOkay-因此,我镜像了您的目录结构,仍然在\Views\Home\index.cshtmlI中为我提供页面。我创建了一个新项目,并尝试使用您的端点配置。这让我直接进入了登录页面。我认为你的问题没有其他原因。看看你是否遗漏了问题中没有提供给我们的其他内容。请参阅我的