【发布时间】:2021-05-11 17:09:20
【问题描述】:
我是 Razor 架构和 ASP.NET Core 的新手,所以如果我遗漏了一些明显的东西,我深表歉意。
我有一个用最新的 .NET Core 框架 .NET 5.0 编写的新 Web 应用程序。我有一个 pg 文件夹,其中包含两个 Razor 页面 Contact.cshtml 和 Index.cshtml。我已将其他指南中的代码复制到我的 Startup.cs 文件中,以尝试显示我的页面,但到目前为止,我只收到 404 错误。这是我的 Startup 类中的 ConfigureServices 和 Configure 方法。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/pg/Index", "");
});
// ----------------------------------------------
services.AddMvc();
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddRazorPages().AddMvcOptions(options => options.EnableEndpointRouting = false);
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(1500);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404)
{
context.Request.Path = "/pg/Index";
await next();
}
});
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=pg}/{action=Index}/{id?}");
});
}
在启动我的应用程序时,我无法显示我的任何一个页面。我已经尝试过 https://localhost:44328/、https://localhost:44328/pg/Index 和 https://localhost:44328/pg/Index.cshtml,但我刚刚返回“找不到网页网址。”
【问题讨论】:
标签: c# asp.net-core razor .net-5