我按照此处MemoryCache给出的步骤来实现ASP.NET Core,当我启动应用程序(dotnet run从命令提示符)时,出现以下错误。
System.InvalidOperationException:尝试激活“Microsoft.AspNetCore.Session.DistributedSessionStore”时无法解析类型“Microsoft.Extensions.Caching.Distributed.IDistributedCache”的服务。
让我困惑的是我正在使用services.AddMemoryCache()而不是 services.AddDistributedMemoryCache()。此bin中提供了完整的堆栈跟踪。我只引用了这些包
Run Code Online (Sandbox Code Playgroud)<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
我的Configure
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseSession();
app.UseCors(
builder => builder
.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin()
.AllowCredentials());
app.UseMvc(
routes =>
{
routes.MapRoute(
"default",
"api/{controller}/{action}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud)
配置服务
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services
.AddMvcCore()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonFormatters();
services.AddMemoryCache();
// Angular …Run Code Online (Sandbox Code Playgroud) 我一直在关注这篇关于在此处编写自定义标记帮助程序的 Microsoft 文章。
在我看到元素标记在 C# 中硬编码的代码的每个地方
示例(取自上述链接)
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "section";
output.Content.SetHtmlContent(
$@"<ul><li><strong>Version:</strong> {Info.Version}</li>
<li><strong>Copyright Year:</strong> {Info.CopyrightYear}</li>
<li><strong>Approved:</strong> {Info.Approved}</li>
<li><strong>Number of tags to show:</strong> {Info.TagsToShow}</li></ul>");
output.TagMode = TagMode.StartTagAndEndTag;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法从 cshtml 文件加载标记模板,而不是这样做?(类似于加载部分视图)
我的目的是拥有单独的cshtml文件(每个元素类型一个),以便我可以轻松地设置它们的样式。我的 C# 看起来也很干净!
谢谢,
詹姆士