Static Id - ASP.NET Core Distributed Cache Tag Helper

Vetrivel mp

We are using ASP.NET Core Distributed Cache Tag Helper with SQL server.

<distributed-cache name="MyCacheItem1" expires-after="TimeSpan.FromDays(1)">
   <p>Something that will be cached</p>
   @DateTime.Now.ToString()
</distributed-cache>

It stores as below. enter image description here

The problem is that Id column is automatically hashed. we want some meaningful string in Id column. Is it possible?

Vetrivel mp

Thank you @Fei Han. I got clue from your answer.

To store custom string in Id column. Follow the steps.

  1. Implement custom class using IDistributedCacheTagHelperService ( I created like HtmlDistributedCacheTagHelperService)

  2. Inject this custom class in startup. services.AddScoped<IDistributedCacheTagHelperService, HtmlDistributedCacheTagHelperService>();

  3. Copy actual source code of DistributedCacheTagHelperService (https://github.com/dotnet/aspnetcore/blob/52eff90fbcfca39b7eb58baad597df6a99a542b0/src/Mvc/Mvc.TagHelpers/src/Cache/DistributedCacheTagHelperService.cs#L102). and paste inside HtmlDistributedCacheTagHelperService.

  4. I added custom attribute htmlcache-uniqueid. <distributed-cache name="UniqueName" htmlcache-uniqueid="CustomStringId">

  5. Inside HtmlDistributedCacheTagHelperService class i added below code to get my custom attribute.

     if (output.Attributes != null && output.Attributes.Count > 0 &&
     string.Equals(output.Attributes[0].Name, "htmlcache-uniqueid") && 
     output.Attributes[0].Value != null)
    {
     storageKey = Convert.ToString(output.Attributes[0].Value);
    }
    
  6. finally you can see id stored in db like below. enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Select Tag Helper in ASP.NET Core MVC

ASP.NET Core Tag Helper Intellisense in Visual Studio 2017

asp.net core tag helper for conditionally add class to an element

ASP .Net Core Custom Tag Helper to Convert CamelCase Properties to spaces

Append QueryString to href in asp.net core Anchor Helper Tag

Unit Testing Tag Helper in ASP.NET Core - Populating Tag Public Properties

.NET Core Distributed Redis Cache with Password

ASP.NET Core Route Tag Helper use Route?

Select ENUM Tag Helper in ASP.NET Core MVC

Getting all of the children Tag Helpers in a parent Tag Helper Asp.Net Core

ASP Core tag helper with inheritance in model

String literals in ASP.NET Core MVC tag helper arguments

Tag helper not being processed in ASP.NET Core 2

Asp.Net Core 2 Complex Tag Helper

ASP.NET Core Script Tag Helper asp-fallback-src fails integrity check

ASP .NET Core 2.1 Razor Pages anchor tag helper generates empty href for asp-route

What is the difference between partial tag helper and HTML helper in asp.net core?

.Net Core 2.1 Cache Tag Helper

Asp-for tag helper in Material Design Lite with .NET Core

Asp.Net Core Cache Tag Helper

SQL distributed cache created on Asp.net core startup

ASP.NET Core : Tag helper "select" is empty

ASP.NET Core custom tag helper is not working

Asp .Net Core Bootstrap 4 Tabs tag helper

Net Core: Use a Tag Helper in a Custom Tag Helper that returns Html?

ASP.NET Core validation tag helper strange behaviour

ASP.NET Core distributed cache with Redis or SQL Server

ASP.NET Core MVC Label Tag Helper

ASP.NET Core asp-fallback tag helper with pseudo selectors (and bootstrap-icons)