我无法让我的 Razor Pages 在我的 ASP.NET 5 应用程序中显示
I cannot get my Razor Pages to display in my ASP.NET 5 application
我是 Razor 架构和 ASP.NET Core 的新手,所以如果我遗漏了一些明显的东西,我深表歉意。
我有一个用最新的 .NET Core 框架 .NET 5.0 编写的新 Web 应用程序。我有一个包含两个 Razor 页面的 pg 文件夹,Contact.cshtml 和 Index.cshtml。我已将其他指南中的代码复制到我的 Startup.cs 文件中以尝试让我的页面显示,但到目前为止我只收到 404 错误。这是我的 Startup class.
中的 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,我只是返回“没有网页找到了网址。"
编辑:这是我的解决方案资源管理器:
app.UseEndpoints(endpoints =>
{
...
endpoints.MapRazorPages();
});
您的 404 特例可能也需要降低;它是否会成为 404 尚未确定。
我已尝试使用您的配置来说明您的问题。它对我来说很好用。可能是你的文件夹结构不好,或者你没有添加需要的控制器。
由于您使用的是 MVC,因此需要将 index.razor
文件放入 Views 文件夹中。文件夹结构应该是这样的 -
您可以查看 official document:
The runtime looks for Razor Pages files in the Pages folder by default.
如果您不想更改 razor pages 文件夹,请将以下代码添加到您的 Startup.cs:
services.AddMvc().AddRazorPagesOptions(opt => {
opt.RootDirectory = "/pg";
});
此外,记得添加 razor pages 端点:
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=pg}/{action=Index}/{id?}");
});
您需要创建一个名为 Pages 的文件夹,并将所有 razor 页面添加到该 Pages 文件夹中。
在配置方法下的startup.cs中添加以下代码
app.UseEndpoints(endpoints =>
{
..
endpoints.MapRazorPages();
});
我是 Razor 架构和 ASP.NET Core 的新手,所以如果我遗漏了一些明显的东西,我深表歉意。
我有一个用最新的 .NET Core 框架 .NET 5.0 编写的新 Web 应用程序。我有一个包含两个 Razor 页面的 pg 文件夹,Contact.cshtml 和 Index.cshtml。我已将其他指南中的代码复制到我的 Startup.cs 文件中以尝试让我的页面显示,但到目前为止我只收到 404 错误。这是我的 Startup class.
中的 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,我只是返回“没有网页找到了网址。"
编辑:这是我的解决方案资源管理器:
app.UseEndpoints(endpoints =>
{
...
endpoints.MapRazorPages();
});
您的 404 特例可能也需要降低;它是否会成为 404 尚未确定。
我已尝试使用您的配置来说明您的问题。它对我来说很好用。可能是你的文件夹结构不好,或者你没有添加需要的控制器。
由于您使用的是 MVC,因此需要将 index.razor
文件放入 Views 文件夹中。文件夹结构应该是这样的 -
您可以查看 official document:
The runtime looks for Razor Pages files in the Pages folder by default.
如果您不想更改 razor pages 文件夹,请将以下代码添加到您的 Startup.cs:
services.AddMvc().AddRazorPagesOptions(opt => {
opt.RootDirectory = "/pg";
});
此外,记得添加 razor pages 端点:
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=pg}/{action=Index}/{id?}");
});
您需要创建一个名为 Pages 的文件夹,并将所有 razor 页面添加到该 Pages 文件夹中。
在配置方法下的startup.cs中添加以下代码
app.UseEndpoints(endpoints =>
{
..
endpoints.MapRazorPages();
});