Files
Umbraco-CMS/src/Umbraco.Web.Common/ModelBinders/ContentModelBinderProvider.cs
Nikolaj Geisle c576bbea03 v10: Fix build warnings in Web.Common (#12349)
* Run code cleanup

* Run dotnet format

* Start manual cleanup in Web.Common

* Finish up manual cleanup

* Fix tests

* Fix up InMemoryModelFactory.cs

* Inject proper macroRenderer

* Update src/Umbraco.Web.Common/Filters/JsonDateTimeFormatAttribute.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update src/Umbraco.Web.Common/Filters/ValidateUmbracoFormRouteStringAttribute.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Fix based on review

Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
2022-05-09 09:39:46 +02:00

31 lines
1.2 KiB
C#

using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
namespace Umbraco.Cms.Web.Common.ModelBinders;
/// <summary>
/// The provider for <see cref="ContentModelBinder" /> mapping view models, supporting mapping to and from any
/// IPublishedContent or IContentModel.
/// </summary>
public class ContentModelBinderProvider : IModelBinderProvider
{
public IModelBinder? GetBinder(ModelBinderProviderContext context)
{
Type modelType = context.Metadata.ModelType;
// Can bind to ContentModel (exact type match)
// or to ContentModel<TContent> (exact generic type match)
// or to TContent where TContent : IPublishedContent (any IPublishedContent implementation)
if (modelType == typeof(ContentModel) ||
(modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(ContentModel<>)) ||
typeof(IPublishedContent).IsAssignableFrom(modelType))
{
return new BinderTypeModelBinder(typeof(ContentModelBinder));
}
return null;
}
}