C# 如何在ASP.NET核心应用程序中使用Razor类库
我编写了一个简单的Razor类库,如下所示 Areas/Library/Pages/_ViewStart.cshtmlC# 如何在ASP.NET核心应用程序中使用Razor类库,c#,asp.net-core,razor,C#,Asp.net Core,Razor,我编写了一个简单的Razor类库,如下所示 Areas/Library/Pages/_ViewStart.cshtml @{ 布局=“_布局”; } 区域/Library/Pages/Shared/\u Login.cshtml 登录 使用本地帐户登录。 电子邮件 密码 还记得我吗? 登录 区域/Library/Pages/Shared/_Message.cshtml 这是一个局部视图 此部分视图被视为Razor类库 区域/Library/Pages/Shared/\u Re
@{
布局=“_布局”;
}
区域/Library/Pages/Shared/\u Login.cshtml
登录
使用本地帐户登录。
电子邮件
密码
还记得我吗?
登录
区域/Library/Pages/Shared/_Message.cshtml
这是一个局部视图
此部分视图被视为Razor类库
区域/Library/Pages/Shared/\u Register.cshtml
登记
创建一个新帐户。
电子邮件
密码
确认密码
登记
Areas/Library/Pages/Page1.cshtml
@page
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
你好,来自RazorUI类库!
Areas/Library/Pages/Page2.cshtml
@page
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
Areas/Library/Pages/Page3.cshtml
@page
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
之后,我将其作为一个依赖项添加到一个新的web应用程序项目中,并更改了布局
WebApp布局
@ViewData[“标题”]-WebApplication1
但是剃须刀页面不起作用
单击没有可用内容,有人可以帮助查找问题吗
示例存储库:问题与您没有为Razor页面配置路由有关。从您的源代码中,我们可以看到,在Razor类库中,您正在创建一些Razor页面,而在WebApplication1中,它只是配置MVC路由,而没有Razor页面路由。因此,它会导致这个问题
要解决此问题,请尝试在WebApplication1的Startup.cs文件中注册Razor页面路由:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages(); //razor page routing
services.AddControllersWithViews();
}
// 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("/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.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages(); //razor page routing
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
然后,屏幕截图如下所示: