2022-05-09 09:39:46 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
2020-05-08 13:23:44 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
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
|
2020-05-08 13:23:44 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
public IModelBinder? GetBinder(ModelBinderProviderContext context)
|
2021-02-18 11:06:02 +01:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
Type modelType = context.Metadata.ModelType;
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// 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));
|
2020-05-08 13:23:44 +02:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
|
|
|
|
|
return null;
|
2020-05-08 13:23:44 +02:00
|
|
|
}
|
|
|
|
|
}
|