This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses #5600
- Loading branch information
Showing
26 changed files
with
950 additions
and
240 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
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
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
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
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
31 changes: 31 additions & 0 deletions
31
src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/SaveTempDataPropertyFilterFactory.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,31 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal | ||
{ | ||
public class SaveTempDataPropertyFilterFactory : IFilterFactory | ||
{ | ||
// Cannot be public as <c>PropertyHelper</c> is an internal shared source type | ||
internal IList<PropertyHelper> TempDataProperties { get; set; } | ||
|
||
public bool IsReusable => false; | ||
|
||
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) | ||
{ | ||
if (serviceProvider == null) | ||
{ | ||
throw new ArgumentNullException(nameof(serviceProvider)); | ||
} | ||
|
||
var service = serviceProvider.GetRequiredService<SaveTempDataPropertyFilter>(); | ||
service.PropertyHelpers = TempDataProperties; | ||
return service; | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/TempDataApplicationModelProvider.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,80 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
using Microsoft.AspNetCore.Mvc.Internal; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal | ||
{ | ||
public class TempDataApplicationModelProvider : IApplicationModelProvider | ||
{ | ||
/// <inheritdoc /> | ||
/// <remarks>This order ensures that <see cref="TempDataApplicationModelProvider"/> runs after the <see cref="DefaultApplicationModelProvider"/>.</remarks> | ||
public int Order => -1000 + 10; | ||
|
||
/// <inheritdoc /> | ||
public void OnProvidersExecuted(ApplicationModelProviderContext context) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void OnProvidersExecuting(ApplicationModelProviderContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
foreach (var controllerModel in context.Result.Controllers) | ||
{ | ||
SaveTempDataPropertyFilterFactory factory = null; | ||
var propertyHelpers = PropertyHelper.GetVisibleProperties(type: controllerModel.ControllerType.AsType()); | ||
for (var i = 0; i < propertyHelpers.Length; i++) | ||
{ | ||
var propertyHelper = propertyHelpers[i]; | ||
if (propertyHelper.Property.IsDefined(typeof(TempDataAttribute))) | ||
{ | ||
ValidateProperty(propertyHelper); | ||
if (factory == null) | ||
{ | ||
factory = new SaveTempDataPropertyFilterFactory() | ||
{ | ||
TempDataProperties = new List<PropertyHelper>() | ||
}; | ||
} | ||
|
||
factory.TempDataProperties.Add(propertyHelper); | ||
} | ||
} | ||
|
||
if (factory != null) | ||
{ | ||
controllerModel.Filters.Add(factory); | ||
} | ||
} | ||
} | ||
|
||
private void ValidateProperty(PropertyHelper propertyHelper) | ||
{ | ||
var property = propertyHelper.Property; | ||
if (!(property.SetMethod != null && | ||
property.SetMethod.IsPublic && | ||
property.GetMethod != null && | ||
property.GetMethod.IsPublic)) | ||
{ | ||
throw new InvalidOperationException( | ||
Resources.FormatTempDataProperties_PublicGetterSetter(property.DeclaringType.FullName, property.Name, nameof(TempDataAttribute))); | ||
} | ||
|
||
if (!(property.PropertyType.GetTypeInfo().IsPrimitive || property.PropertyType == typeof(string))) | ||
{ | ||
throw new InvalidOperationException( | ||
Resources.FormatTempDataProperties_PrimitiveTypeOrString(property.DeclaringType.FullName, property.Name, nameof(TempDataAttribute))); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.