add metadata module for razor pages
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
namespace zero.Metadata;
|
||||
|
||||
public class Metadata
|
||||
{
|
||||
public string AppVersion { get; set; }
|
||||
|
||||
public string Title { get; set; }
|
||||
|
||||
public string PageName { get; set; }
|
||||
|
||||
public string Description { get; set; }
|
||||
|
||||
public string Image { get; set; }
|
||||
|
||||
public string Robots { get; set; }
|
||||
|
||||
public string Icon { get; set; }
|
||||
|
||||
public string Url { get; set; }
|
||||
|
||||
public string Author { get; set; }
|
||||
|
||||
//public string Schema { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace zero.Metadata;
|
||||
|
||||
public class ZeroMetadataModule : ZeroModule
|
||||
{
|
||||
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddScoped<IMetadataService, MetadataService>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace zero.Metadata;
|
||||
|
||||
public class MetadataOptions
|
||||
{
|
||||
public List<string> TitleFragments { get; set; } = [];
|
||||
|
||||
public bool? NoIndex { get; set; }
|
||||
|
||||
public bool? NoFollow { get; set; }
|
||||
|
||||
public string Description { get; set; }
|
||||
|
||||
public string Icon { get; set; }
|
||||
|
||||
public string Image { get; set; }
|
||||
|
||||
public string Author { get; set; }
|
||||
|
||||
//public Schema Schema { get; set; }
|
||||
|
||||
public MetadataOptions() { }
|
||||
|
||||
public MetadataOptions(params string[] titleFragments)
|
||||
{
|
||||
TitleFragments.AddRange(titleFragments);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Reflection;
|
||||
|
||||
namespace zero.Metadata;
|
||||
|
||||
public class MetadataService : IMetadataService
|
||||
{
|
||||
protected ILogger<MetadataService> Logger { get; private set; }
|
||||
|
||||
protected ILocalizer Localizer { get; private set; }
|
||||
|
||||
|
||||
public MetadataService(ILogger<MetadataService> logger, ILocalizer localizer)
|
||||
{
|
||||
Logger = logger;
|
||||
Localizer = localizer;
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public Metadata Generate(string pageName, MetadataOptions options)
|
||||
{
|
||||
Metadata model = new()
|
||||
{
|
||||
Description = options.Description,
|
||||
Icon = options.Icon,
|
||||
Image = options.Image,
|
||||
Author = options.Author,
|
||||
PageName = pageName,
|
||||
AppVersion = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion
|
||||
};
|
||||
|
||||
// generate robots
|
||||
bool noIndex = options.NoIndex ?? false;
|
||||
bool noFollow = options.NoFollow ?? false;
|
||||
model.Robots = String.Format("{0},{1}", noIndex ? "noindex" : "index", noFollow ? "nofollow" : "follow");
|
||||
|
||||
// title
|
||||
HashSet<string> fragments = options.TitleFragments.Where(x => !x.IsNullOrWhiteSpace()).Reverse().ToHashSet();
|
||||
model.Title = !fragments.Any() ? pageName : (String.Join(" / ", fragments.Select(fragment => Localizer.Maybe(fragment))) + " / " + pageName);
|
||||
|
||||
// add schema
|
||||
//if (options.Schema != null)
|
||||
//{
|
||||
// model.Schema = options.Schema.ToJson();
|
||||
//}
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface IMetadataService
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates metadata.
|
||||
/// </summary>
|
||||
Metadata Generate(string pageName, MetadataOptions options);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
//using Microsoft.AspNetCore.Mvc;
|
||||
//using System.Dynamic;
|
||||
|
||||
//namespace zero.Metadata;
|
||||
|
||||
//public class Schema
|
||||
//{
|
||||
// public dynamic Model { get; private set; }
|
||||
|
||||
// static JsonOptions Settings;
|
||||
|
||||
// static Schema()
|
||||
// {
|
||||
// Settings = new JsonOptions()
|
||||
// {
|
||||
// Formatting = Formatting.None,
|
||||
// TypeNameHandling = TypeNameHandling.None
|
||||
// };
|
||||
// Settings.Converters.Add(new SchemaConverter());
|
||||
// }
|
||||
|
||||
|
||||
// public Schema(string type, Action<dynamic> set) : this(type, false, set) { }
|
||||
|
||||
// public Schema(string type, bool isRoot, Action<dynamic> set)
|
||||
// {
|
||||
// Model = new ExpandoObject();
|
||||
// var dict = Model as IDictionary<string, object>;
|
||||
// if (isRoot)
|
||||
// {
|
||||
// dict["@context"] = "https://schema.org";
|
||||
// }
|
||||
// dict["@type"] = type;
|
||||
// set(Model);
|
||||
// }
|
||||
|
||||
|
||||
// public string ToJson() => JsonConvert.SerializeObject(Model, Settings);
|
||||
|
||||
|
||||
// class SchemaConverter : JsonConverter<Schema>
|
||||
// {
|
||||
// public override void WriteJson(JsonWriter writer, Schema value, JsonSerializer serializer)
|
||||
// {
|
||||
// serializer.Serialize(writer, value.Model);
|
||||
// }
|
||||
|
||||
// public override Schema ReadJson(JsonReader reader, Type objectType, Schema existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
// {
|
||||
// throw new NotImplementedException();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Net.Mime;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using zero.Metadata;
|
||||
|
||||
namespace zero.Mvc;
|
||||
|
||||
@@ -17,4 +17,9 @@ public abstract class ZeroPageModel : PageModel
|
||||
/// </summary>
|
||||
public ILocalizer Localizer => _localizer ?? (_localizer = HttpContext?.RequestServices?.GetService<ILocalizer>());
|
||||
ILocalizer _localizer;
|
||||
|
||||
/// <summary>
|
||||
/// Set metadata for this page
|
||||
/// </summary>
|
||||
public MetadataOptions Metadata { get; protected set; } = new();
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using zero.Mails;
|
||||
using zero.Metadata;
|
||||
using zero.Mvc;
|
||||
using zero.Numbers;
|
||||
using zero.Routing;
|
||||
@@ -60,6 +61,7 @@ public class ZeroBuilder
|
||||
Modules.Add<ZeroRenderingModule>();
|
||||
Modules.Add<ZeroNumberModule>();
|
||||
Modules.Add<ZeroRoutingModule>();
|
||||
Modules.Add<ZeroMetadataModule>();
|
||||
|
||||
Modules.ConfigureServices(services, configuration);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user