Skip to content

Commit

Permalink
removed ViewContext' ViewData and TempData (align with aspnetcore)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtoroq committed Jan 29, 2021
1 parent fe26911 commit a675d21
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
36 changes: 5 additions & 31 deletions src/Xcst.AspNet/Framework/ViewContext.cs
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#if NETCOREAPP
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
#endif

namespace System.Web.Mvc {

Expand Down Expand Up @@ -39,42 +41,14 @@ public class ViewContext : ControllerContext {
set => HttpContext.Items[_formContextKey] = value;
}

#if !NETCOREAPP
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "The property setter is only here to support mocking this type and should not be called at runtime.")]
public virtual ViewDataDictionary? ViewData { get; set; }

[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "The property setter is only here to support mocking this type and should not be called at runtime.")]
public virtual TempDataDictionary? TempData { get; set; }
#endif

// parameterless constructor used for mocking
public ViewContext() { }

public ViewContext(
#if NETCOREAPP
Microsoft.AspNetCore.Http.HttpContext httpContext
#else
HttpContextBase httpContext
#endif
) : base(httpContext) { }
public ViewContext(HttpContextBase httpContext)
: base(httpContext) { }

#if NETCOREAPP
public ViewContext(ControllerContext controllerContext)
: base(controllerContext) { }
#else
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "The virtual property setters are only to support mocking frameworks, in which case this constructor shouldn't be called anyway.")]
public ViewContext(ControllerContext controllerContext, ViewDataDictionary? viewData, TempDataDictionary? tempData)
: base(controllerContext) {

if (controllerContext is null) throw new ArgumentNullException(nameof(controllerContext));
// When cloning, ViewData/TempData can be null if ViewContext was initialized with HttpContextBase only
//if (viewData is null) throw new ArgumentNullException(nameof(viewData));
//if (tempData is null) throw new ArgumentNullException(nameof(tempData));

this.ViewData = viewData;
this.TempData = tempData;
}
#endif

internal FormContext? GetFormContextForClientValidation() =>
(this.ClientValidationEnabled) ? this.FormContext : null;
Expand Down
4 changes: 0 additions & 4 deletions src/Xcst.AspNet/FrameworkExtensions.cs
Expand Up @@ -109,12 +109,8 @@ static class FrameworkExtensions {
context
#if ASPNETMVC
, view ?? context.View
#endif
#if !NETCOREAPP
, viewData ?? context.ViewData
, tempData ?? context.TempData
#endif
#if ASPNETMVC
, writer ?? context.Writer
#endif
) {
Expand Down
10 changes: 9 additions & 1 deletion src/Xcst.AspNet/Mvc/XcstViewPage.cs
Expand Up @@ -43,9 +43,11 @@ public abstract class XcstViewPage : XcstPage, IViewDataContainer {
Context = value?.HttpContext;
#pragma warning restore CS8601

#if ASPNETMVC
if (value?.ViewData is ViewDataDictionary vd) {
ViewData = vd;
}
#endif

#pragma warning disable CS8625
Url = null;
Expand Down Expand Up @@ -104,7 +106,11 @@ public abstract class XcstViewPage : XcstPage, IViewDataContainer {
public ModelStateDictionary ModelState => ViewData.ModelState;

public virtual TempDataDictionary TempData {
get => _tempData ??= ViewContext?.TempData ?? new TempDataDictionary();
get => _tempData ??=
#if ASPNETMVC
ViewContext?.TempData ??
#endif
new TempDataDictionary();
set => _tempData = value;
}

Expand Down Expand Up @@ -206,8 +212,10 @@ public abstract class XcstViewPage : XcstPage, IViewDataContainer {
view: new XcstView(this.ViewContext, viewPage.VirtualPath),
#endif
viewData: (_viewData != null) ? new ViewDataDictionary(_viewData)
#if ASPNETMVC
// Never use this.ViewContext.ViewData
: (this.ViewContext.ViewData != null) ? new ViewDataDictionary()
#endif
: null,
tempData: _tempData
);
Expand Down

0 comments on commit a675d21

Please sign in to comment.