Skip to content

Commit

Permalink
AspNetCore2 + AspMvc5: both can build now
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrock committed Aug 14, 2023
1 parent 894ca73 commit bbcefb0
Show file tree
Hide file tree
Showing 34 changed files with 2,692 additions and 169 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<RootNamespace>SrkToolkit.Domain.AspNetCore2</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0.147-preview2</Version>
Expand All @@ -19,6 +18,7 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\SrkToolkit.snk</AssemblyOriginatorKeyFile>
<PackageReadmeFile>SrkToolkit.Domain.md</PackageReadmeFile>
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

namespace SrkToolkit.Web.Fakes
{
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Web;

/// <summary>
/// Implementation of <see cref="HttpContextBase"/> where the developer can set any property to any value.
/// </summary>
public class BasicHttpContext : HttpContext
public class BasicHttpContext : HttpContextBase
{
private HttpSessionStateBase session;
private IPrincipal user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace SrkToolkit.Web
{
using Microsoft.AspNetCore.Mvc;
using System;
using System.Diagnostics;
using System.Runtime.Serialization.Json;
Expand Down Expand Up @@ -60,7 +59,7 @@ public JsonNetResult()
/// <value>
/// The JSON serializer.
/// </value>
public static Action<object, HttpResponse> Serializer { get; set; }
public static Action<object, HttpResponseBase> Serializer { get; set; }

/// <summary>
/// Gets or sets the content encoding.
Expand Down
20 changes: 15 additions & 5 deletions Sources/SrkToolkit.Web.AspMvc5/Open/PageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,13 @@ public override string ToString()
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public string ToString(PageInfoObjectSection sections, bool indented)
{
var sb = new StringBuilder();
public string ToString(PageInfoObjectSection sections, bool indented)
{
#if ASPMVCCORE
var sb = new StringWriter();
#elif ASPMVC
var sb = new StringBuilder();
#endif
this.Write(sb, sections, indented);
return sb.ToString();
}
Expand All @@ -350,8 +354,14 @@ public string ToString(PageInfoObjectSection sections, bool indented)
/// <param name="sb">The sb.</param>
/// <param name="sections">the desires sections</param>
/// <param name="indented">if true the generated html will be indented</param>
public void Write(StringBuilder sb, PageInfoObjectSection sections, bool indented)
{
public void Write(
#if ASPMVCCORE
StringWriter sb,
#elif ASPMVC
StringBuilder sb,
#endif
PageInfoObjectSection sections, bool indented)
{
if (sb == null)
throw new ArgumentNullException("sb");

Expand Down
30 changes: 24 additions & 6 deletions Sources/SrkToolkit.Web.AspMvc5/Open/PageInfoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
/// </returns>
public override string ToString()
{
var sb = new StringBuilder();
#if ASPMVCCORE
var sb = new StringWriter();
#elif ASPMVC
var sb = new StringBuilder();
#endif
this.ToString(sb, PageInfo.defaultSections, false);
return sb.ToString();
}
Expand All @@ -125,7 +129,13 @@ public override string ToString()
/// <param name="sb">The <see cref="StringBuilder"/> to write to.</param>
/// <param name="sections">The sections to use.</param>
/// <exception cref="System.ArgumentNullException">sb</exception>
public void ToString(StringBuilder sb, PageInfoObjectSection sections, bool indented)
public void ToString(
#if ASPMVCCORE
StringWriter sb,
#elif ASPMVC
StringBuilder sb,
#endif
PageInfoObjectSection sections, bool indented)
{
if (sb == null)
throw new ArgumentNullException("sb");
Expand All @@ -135,10 +145,18 @@ public void ToString(StringBuilder sb, PageInfoObjectSection sections, bool inde
if ((obj.Section & sections) != 0)
{
obj.SetValue(this.Value);
if (indented)
sb.AppendLine(obj.ToString());
else
sb.Append(obj.ToString());

#if ASPMVCCORE
if (indented)
sb.WriteLine(obj.ToString());
else
sb.Write(obj.ToString());
#elif ASPMVC
if (indented)
sb.AppendLine(obj.ToString());
else
sb.Append(obj.ToString());
#endif
}
}
}
Expand Down
83 changes: 58 additions & 25 deletions Sources/SrkToolkit.Web.AspMvc5/Open/PageInfoObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@

namespace SrkToolkit.Web.Open
{
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#if ASPMVCCORE

#else
using System.Web.Mvc;
#endif
#if ASPMVCCORE
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Text.Encodings.Web;
#endif

#if ASPMVC
using System.Web.Mvc;
#endif

/// <summary>
/// A HTML element that will represent a page information .
Expand Down Expand Up @@ -80,9 +82,6 @@ public OpenGraphTag OpenGraphTag
get { return this.openGraphTag; }
}

#if ASPNETCORE
#endif
#if !NSTD && !NET40
private TagBuilder Tag
{
get
Expand All @@ -98,14 +97,17 @@ private TagBuilder Tag

if (this.tagValue != null)
{
////tag.SetInnerText(this.tagValue); // net40
tag.InnerHtml.Append(this.tagValue); // netstandard2.0
#if ASPMVCCORE
tag.InnerHtml.Append(this.tagValue); // netstandard2.0
#endif
#if ASPMVC
tag.SetInnerText(this.tagValue); // net40
#endif
}

return tag;
}
}
#endif

/// <summary>
/// Prepare a HTML element of the specified name to contain the item's value.
Expand Down Expand Up @@ -216,28 +218,59 @@ public PageInfoObject SetValue(string value)
/// </returns>
public override string ToString()
{
#if ASPMVCCORE
var sb = new StringWriter();
#elif ASPMVC
var sb = new StringBuilder();
#endif
this.ToString(sb);
return sb.ToString();
}

/// <summary>
/// Returns a <see cref="System.String" /> that contains the generated HTML tags.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that contains the generated HTML tags.
/// </returns>
public void ToString(
#if ASPMVCCORE
StringWriter sb
#elif ASPMVC
StringBuilder sb
#endif
)
{
#if NSTD || NET40
return "<!-- PageInfoObject: NOT IMPLEMENTED IN NETSTANDARD -->";
#else
var tag = this.Tag;
if (this.openGraphTag != null)
{
return this.openGraphTag.ToString();
#if ASPMVCCORE
sb.Write(this.openGraphTag.ToString());
#else
sb.Append(this.openGraphTag.ToString());
#endif
}
else if (tag != null)
{
#if ASPMVCCORE
return tag.ToString();
#else
return tag.ToString(singleTagNames.Contains(this.tagName) ? TagRenderMode.SelfClosing : TagRenderMode.Normal);
#endif
}
else
{
return "<!-- PageInfoObject: no HTML to write -->";
}
#endif
}
#if ASPMVCCORE
tag.WriteTo(sb, HtmlEncoder.Default);
#else
sb.Append(tag.ToString(singleTagNames.Contains(this.tagName) ? TagRenderMode.SelfClosing : TagRenderMode.Normal));
#endif
}
else
{
#if ASPMVCCORE
sb.Write("<!-- PageInfoObject: no HTML to write -->");
#else
sb.Append("<!-- PageInfoObject: no HTML to write -->");
#endif
}
#endif
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@
using System.Runtime.InteropServices;
using System;

[assembly: AssemblyTitle("SrkToolkit.Web.Mvc5")]
[assembly: AssemblyDescription("SrkToolkit.Web for .NET 4.5 and ASP MVC 5")]

[assembly: CLSCompliant(true)]
[assembly: Guid("a2fdfb36-514d-43ac-8524-a6ace628b7d6")]
110 changes: 110 additions & 0 deletions Sources/SrkToolkit.Web.AspMvc5/Services/BaseSessionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// Copyright 2014 SandRock
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

namespace SrkToolkit.Web.Services
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///
/// </summary>
public class BaseSessionService
{
private readonly ISessionServiceSource source;

/// <summary>
/// Initializes an instance with a HttpSessionStateBase object (likely from ASP MVC).
/// </summary>
/// <param name="httpSessionStateBase"></param>
public BaseSessionService(HttpSessionStateBase httpSessionStateBase)
{
this.source = new HttpBaseSessionServiceSource(httpSessionStateBase);
}

/// <summary>
/// Initializes a new instance of the <see cref="BaseSessionService"/> class.
/// </summary>
/// <param name="httpSessionDictionary">The HTTP session dictionary.</param>
public BaseSessionService(IDictionary<string, object> httpSessionDictionary)
{
this.source = new DictionarySessionServiceSource(httpSessionDictionary);
}

/// <summary>
/// Clears everything form the session.
/// </summary>
public void Clear()
{
this.source.Clear();
}

/// <summary>
/// Clears a session value.
/// </summary>
/// <param name="key"></param>
protected void Clear(string key)
{
this.source.Clear(key);
}

/// <summary>
/// Sets a session value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
protected void Set<T>(string key, T value)
{
this.source.Set(key, value);
}


/// <summary>
/// Gets a session reference-type value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">The key.</param>
/// <returns></returns>
protected T GetObject<T>(string key)
where T : class
{
var obj = this.source.Get(key);
if (obj == null)
return null;

return (T)obj;
}

/// <summary>
/// Gets a session nullable value-type value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">The key.</param>
/// <returns></returns>
protected T? GetValue<T>(string key)
where T : struct
{
var obj = this.source.Get(key);
if (obj == null)
return default(T?);

return (T)obj;
}
}
}
Loading

0 comments on commit bbcefb0

Please sign in to comment.