forked from dotnetcore/osharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip:(mvc) 添加雪花Long类型Id的ModelBinder转换器,并添加到Mvc服务中
- Loading branch information
Showing
5 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/OSharp.AspNetCore/Mvc/ModelBinding/LongIdModelBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/OSharp.AspNetCore/Mvc/ModelBinding/LongIdModelBinderProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters