Skip to content

Commit

Permalink
wip:(mvc) 添加雪花Long类型Id的ModelBinder转换器,并添加到Mvc服务中
Browse files Browse the repository at this point in the history
  • Loading branch information
gmf520 committed Oct 4, 2023
1 parent ab559e9 commit e962d15
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
7 changes: 5 additions & 2 deletions samples/web/Liuliu.Demo.WebApi/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"Logging": {
"LogLevel": {
"Default": "Debug",
"OSharp": "Debug",
"Liuliu": "Debug",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning"
}
Expand Down Expand Up @@ -115,13 +117,14 @@
},
"Mvc": {
"IsLowercaseJsonProperty": true,
"IsLowercaseUrls": true
"IsLowercaseUrls": true,
"IsLongIdModelBinder": false
},
"Cors": {
"PolicyName": "MyCors",
"AllowAnyHeader": true,
"WithMethods": [ "POST", "PUT", "DELETE" ],
"WithOrigins": [ ],
"WithOrigins": [],
"Enabled": true
},
"Redis": {
Expand Down
44 changes: 44 additions & 0 deletions src/OSharp.AspNetCore/Mvc/ModelBinding/LongIdModelBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -----------------------------------------------------------------------
// <copyright file="SnowflakeIdModelBinder.cs" company="OSharp开源团队">
// Copyright (c) 2014-2023 OSharp. All rights reserved.
// </copyright>
// <site>http://www.osharp.org</site>
// <last-editor>郭明锋</last-editor>
// <last-date>2023-10-05 0:19</last-date>
// -----------------------------------------------------------------------

using System.Globalization;


namespace OSharp.AspNetCore.Mvc.ModelBinding
{
public class LongIdModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}

// 获取传入的ID值作为字符串
string valueAsString = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).FirstValue;
if (string.IsNullOrEmpty(valueAsString))
{
return Task.CompletedTask;
}

if (long.TryParse(valueAsString, NumberStyles.Integer, CultureInfo.InvariantCulture, out long id))
{
// 成功解析为long,将其设置为模型的值
bindingContext.Result = ModelBindingResult.Success(id);
}
else
{
// 解析失败,返回错误
bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, "Invalid Snowflake ID format.");
}
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// -----------------------------------------------------------------------
// <copyright file="LongIdModelBinderProvider.cs" company="OSharp开源团队">
// Copyright (c) 2014-2023 OSharp. All rights reserved.
// </copyright>
// <site>http://www.osharp.org</site>
// <last-editor>郭明锋</last-editor>
// <last-date>2023-10-05 1:26</last-date>
// -----------------------------------------------------------------------

namespace OSharp.AspNetCore.Mvc.ModelBinding
{
public class LongIdModelBinderProvider : IModelBinderProvider
{
/// <summary>
/// Creates a <see cref="T:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder" /> based on <see cref="T:Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderProviderContext" />.
/// </summary>
/// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderProviderContext" />.</param>
/// <returns>An <see cref="T:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder" />.</returns>
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context.Metadata.ModelType == typeof(long))
{
return new LongIdModelBinder();
}

return null;
}
}
}
11 changes: 9 additions & 2 deletions src/OSharp.AspNetCore/Mvc/MvcPackBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using OSharp.AspNetCore.Cors;
using OSharp.AspNetCore.Mvc.Filters;
using OSharp.AspNetCore.Mvc.ModelBinding;
using OSharp.Core.Options;


Expand Down Expand Up @@ -46,7 +47,13 @@ public override IServiceCollection AddServices(IServiceCollection services)
_corsInitializer.AddCors(services);

OsharpOptions osharp = services.GetOsharpOptions();
services.AddControllersWithViews()
services.AddControllersWithViews(opts =>
{
if (osharp.Mvc?.IsLongIdModelBinder == true)
{
opts.ModelBinderProviders.Insert(0, new LongIdModelBinderProvider());
}
})
.AddControllersAsServices()
.AddNewtonsoftJson(options =>
{
Expand Down Expand Up @@ -80,4 +87,4 @@ public override void UsePack(WebApplication app)

IsEnabled = true;
}
}
}
9 changes: 7 additions & 2 deletions src/OSharp/Core/Options/MvcOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// <copyright file="MvcOptions.cs" company="OSharp开源团队">
// Copyright (c) 2014-2021 OSharp. All rights reserved.
// </copyright>
Expand All @@ -23,4 +23,9 @@ public class MvcOptions
/// 获取或设置 是否URL小写,默认:false
/// </summary>
public bool IsLowercaseUrls { get; set; } = false;
}

/// <summary>
/// 获取或设置 是否使用雪花Id模型绑定器,默认:false
/// </summary>
public bool IsLongIdModelBinder { get; set; } = false;
}

0 comments on commit e962d15

Please sign in to comment.