你可以这样做,
一种方法是制作“你自己的”html 标签(更像是现有的扩展方法)并实现TagHelper
您可以在覆盖时访问或更改内容,我有一个扩展几个 html 标记的类来管理客户全球化,看看代码,看看这是否对您有帮助。
在您的页面中,您可以通过添加您的属性来“拥有”一个 html,就像我在这里用我的属性 cats-language-key 演示的那样:
<div cats-language-key="Home-S2-h1-p1">
</p>
[HtmlTargetElement("p",Attributes = CatsLanguageKey)]
[HtmlTargetElement("span", Attributes = CatsLanguageKey)]
[HtmlTargetElement("a", Attributes = CatsLanguageKey)]
[HtmlTargetElement("li", Attributes = CatsLanguageKey)]
[HtmlTargetElement("h1", Attributes = CatsLanguageKey)]
[HtmlTargetElement("h2", Attributes = CatsLanguageKey)]
[HtmlTargetElement("h3", Attributes = CatsLanguageKey)]
[HtmlTargetElement("h4", Attributes = CatsLanguageKey)]
[HtmlTargetElement("div", Attributes = CatsLanguageKey)]
public class LanguageTagHelper: TagHelper
{
private const string CatsLanguageKey= "cats-language-key";
private readonly ILanguageRepository _repository;
private readonly ClaimsPrincipal _user;
private readonly IMemoryCache _memoryCache;
public LanguageTagHelper(ILanguageRepository repository, IHttpContextAccessor context, IMemoryCache memoryCache)
{
_repository = repository;
_user = context.HttpContext.User;
_memoryCache = memoryCache;
}
[HtmlAttributeName(CatsLanguageKey)]
public string Key { get; set; }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var childContent = await output.GetChildContentAsync();
if (!childContent.IsEmptyOrWhiteSpace)
{
var textItem = _repository.GetHtml(Key, childContent.GetContent().Trim());
if (_user.Identity.IsAuthenticated && _user.IsInRole(MagicStrings.ROLE_TEXTER))
{
output.Attributes.Add("data-language-target", textItem.Language);
output.Attributes.Add("data-language-key", textItem.Key);
var html = new HtmlString(textItem.Text);
output.Content.SetHtmlContent(html);
_memoryCache.Remove(Key);
}
else
{
string text = string.Empty;
if (!_memoryCache.TryGetValue(Key, out text))
{
text = Regex.Replace(textItem.Text, @">\s+<", "><", RegexOptions.Compiled | RegexOptions.Multiline);
text = Regex.Replace(text, @"<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->)(.|\n))*-->", "", RegexOptions.Compiled | RegexOptions.Multiline);
text = Regex.Replace(text, @"^\s+", "", RegexOptions.Compiled | RegexOptions.Multiline);
text = Regex.Replace(text, @"\r\n?|\n", "", RegexOptions.Compiled | RegexOptions.Multiline);
text = Regex.Replace(text, @"\s+", " ", RegexOptions.Compiled | RegexOptions.Multiline);
_memoryCache.Set(Key, text, new MemoryCacheEntryOptions() { Priority= CacheItemPriority.Low, SlidingExpiration= new TimeSpan(hours:1,minutes:0,seconds:0) });
}
var html = new HtmlString(text);
output.Content.SetHtmlContent(html);
}
}
}
}
我在更改整个页面时必须做的另一件事是向我的 MidleWare 添加一些功能。我们注意到我们的页面返回的 HTML 相当臃肿,在不需要的地方有空字符串和填充,然后我缩小了页面(在 JavaScript 中保留了空格和换行符)
public static class BuilderExtensions
{
public static IApplicationBuilder UseHTMLMinification(this IApplicationBuilder app)
{
return app.UseMiddleware<HtmlMinificationMiddleware>();
}
public static IApplicationBuilder UseHTMLMinification(this IApplicationBuilder app,
string excludeFilter)
{
var options = new HtmlMinificationOptions() { ExcludeFilter = excludeFilter };
return app.UseMiddleware<HtmlMinificationMiddleware>(options);
}
public static IApplicationBuilder UseHTMLMinification(this IApplicationBuilder app,
HtmlMinificationOptions minificationOptions)
{
return app.UseMiddleware<HtmlMinificationMiddleware>(minificationOptions);
}
///so other options
}
HtmlMinificationMiddleware 看起来像这样,请注意您不会拥有 StatsRepository,但您可以从示例中编辑它或将其替换为您自己的,StatsRepository 维护的页面统计信息比 google 在没有所有隐私法公开的情况下更详细带有谷歌的 AddSence 或 AWStats 并且是实时的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using System.IO;
using System.Threading.Tasks;
using System.Text;
using System.Text.RegularExpressions;
namespace CATS.Web.Shared.Infrastructure.Middleware
{
using CATS.Web.Shared.Repositories;
public class HtmlMinificationMiddleware
{
private RequestDelegate _next;
StatsRepository _stats;
private HtmlMinificationOptions _minificationOptions;
public HtmlMinificationMiddleware(RequestDelegate next, StatsRepository stats)
: this(next, null, stats)
{
}
public HtmlMinificationMiddleware(RequestDelegate next, HtmlMinificationOptions minificationOptions, StatsRepository stats)
{
_next = next;
_minificationOptions = minificationOptions;
_stats = stats;
}
public async Task Invoke(HttpContext context)
{
var stream = context.Response.Body;
if (_minificationOptions != null)
{
var filter = _minificationOptions.ExcludeFilter;
if (Regex.IsMatch(context.Request.Path, filter))
{
await _next(context);
return;
}
}
long size = 0;
try
{
using (var buffer = new MemoryStream())
{
context.Response.Body = buffer;
await _next(context);
var isHtml = context.Response.ContentType?.ToLower().Contains("text/html");
buffer.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(buffer))
{
string responseBody = await reader.ReadToEndAsync();
var backup = string.Copy(responseBody);
if (context.Response.StatusCode == 200 && isHtml.GetValueOrDefault())
{
try
{
responseBody = Regex.Replace(responseBody, @">\s+<", "><", RegexOptions.Compiled | RegexOptions.Multiline);
responseBody = Regex.Replace(responseBody, @"<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->)(.|\n))*-->", "", RegexOptions.Compiled | RegexOptions.Multiline);
responseBody = Regex.Replace(responseBody, @"\r\n?|\n", "", RegexOptions.Compiled | RegexOptions.Multiline);
responseBody = Regex.Replace(responseBody, @"\s+", " ", RegexOptions.Compiled | RegexOptions.Multiline);
if (string.IsNullOrWhiteSpace(responseBody))
responseBody = backup;
} catch
{
responseBody = backup;
}
}
var bytes = Encoding.UTF8.GetBytes(responseBody);
using (var memoryStream = new MemoryStream(bytes))
{
memoryStream.Seek(0, SeekOrigin.Begin);
await memoryStream.CopyToAsync(stream);
}
size = bytes.LongLength;
await _stats.UpdateRequestSize(context, size);
}
}
}
finally
{
context.Response.Body = stream;
}
}
}
}
public class HtmlMinificationOptions
{
public string ExcludeFilter { get; set; }
}
我做的管道配置是这样的:
namespace CATS.Web.Shared.Infrastructure.Middleware
{
using Microsoft.AspNetCore.Builder;
public class HtmlMinificationPipeline
{
public void Configure(IApplicationBuilder applicationBuilder)
{
applicationBuilder.UseHTMLMinification();
}
}
}
所以,我给了你 2 个选项 1 在标签级别说一个 div,另一个基本上是你喜欢的大小。