Added ControllerExtensions to be used instead of HtmlViewRenderer for net8
Added HttpException class for missing HttpException for net8 Wrapped HttpContext.Session, GetJsonResult, IsAjaxRequest and GetUserIdentityName in controllers for net8 Added AuthenticationService to test Fab2ApprovalMKLink code for net8 Compile conditionally flags to debug in dotnet core
This commit is contained in:
@ -1,43 +1,47 @@
|
||||
#if !NET8
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Security;
|
||||
#endif
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="BinaryContentResult.cs" company="SemanticArchitecture">
|
||||
// http://www.SemanticArchitecture.net pkalkie@gmail.com
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// An ActionResult used to send binary data to the browser.
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
#if NET8
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
#endif
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator {
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
#if !NET8
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// An ActionResult used to send binary data to the browser.
|
||||
/// </summary>
|
||||
public class BinaryContentResult : ActionResult {
|
||||
private readonly string contentType;
|
||||
private readonly byte[] contentBytes;
|
||||
namespace Fab2ApprovalSystem.PdfGenerator;
|
||||
|
||||
public BinaryContentResult(byte[] contentBytes, string contentType) {
|
||||
this.contentBytes = contentBytes;
|
||||
this.contentType = contentType;
|
||||
}
|
||||
public class BinaryContentResult : ActionResult {
|
||||
private readonly string contentType;
|
||||
private readonly byte[] contentBytes;
|
||||
|
||||
public override void ExecuteResult(ControllerContext context) {
|
||||
var response = context.HttpContext.Response;
|
||||
response.Clear();
|
||||
response.Cache.SetCacheability(HttpCacheability.Public);
|
||||
response.ContentType = this.contentType;
|
||||
public BinaryContentResult(byte[] contentBytes, string contentType) {
|
||||
this.contentBytes = contentBytes;
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
using (var stream = new MemoryStream(this.contentBytes)) {
|
||||
stream.WriteTo(response.OutputStream);
|
||||
stream.Flush();
|
||||
}
|
||||
#if !NET8
|
||||
public override void ExecuteResult(ControllerContext context) {
|
||||
var response = context.HttpContext.Response;
|
||||
response.Clear();
|
||||
response.Cache.SetCacheability(HttpCacheability.Public);
|
||||
response.ContentType = contentType;
|
||||
|
||||
using (var stream = new MemoryStream(contentBytes)) {
|
||||
stream.WriteTo(response.OutputStream);
|
||||
stream.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
}
|
@ -1,28 +1,36 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
#if !NET8
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Security;
|
||||
#endif
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="FakeView.cs" company="SemanticArchitecture">
|
||||
// http://www.SemanticArchitecture.net pkalkie@gmail.com
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Defines the FakeView type.
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
#if NET8
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.AspNetCore.Mvc.ViewEngines;
|
||||
#endif
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator {
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Web.Mvc;
|
||||
#if !NET8
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Security.Claims;
|
||||
#endif
|
||||
|
||||
public class FakeView : IView {
|
||||
#region IView Members
|
||||
namespace Fab2ApprovalSystem.PdfGenerator;
|
||||
|
||||
public void Render(ViewContext viewContext, TextWriter writer) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public class FakeView : IView {
|
||||
|
||||
#endregion
|
||||
public void Render(ViewContext viewContext, TextWriter writer) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
#if NET8
|
||||
string IView.Path => throw new NotImplementedException();
|
||||
Task IView.RenderAsync(ViewContext context) => throw new NotImplementedException();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
@ -1,49 +1,59 @@
|
||||
|
||||
#if !NET8
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Mvc.Html;
|
||||
using System.Web.Security;
|
||||
#endif
|
||||
|
||||
#if NET8
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
#endif
|
||||
|
||||
#if !NET8
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator;
|
||||
|
||||
public class HtmlViewRenderer {
|
||||
|
||||
public string RenderViewToString(Controller controller, string viewName, object viewData) {
|
||||
StringBuilder renderedView = new();
|
||||
#if !NET8
|
||||
using (StringWriter responseWriter = new(renderedView)) {
|
||||
HttpResponse fakeResponse = new(responseWriter);
|
||||
HttpContext fakeContext = new(HttpContext.Current.Request, fakeResponse);
|
||||
ControllerContext fakeControllerContext = new(new HttpContextWrapper(fakeContext), controller.ControllerContext.RouteData, controller.ControllerContext.Controller);
|
||||
|
||||
var oldContext = HttpContext.Current;
|
||||
HttpContext.Current = fakeContext;
|
||||
|
||||
using (var viewPage = new ViewPage()) {
|
||||
HtmlHelper html = new HtmlHelper(CreateViewContext(responseWriter, fakeControllerContext), viewPage);
|
||||
html.RenderPartial(viewName, viewData);
|
||||
HttpContext.Current = oldContext;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return renderedView.ToString();
|
||||
}
|
||||
|
||||
#if !NET8
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="HtmlViewRenderer.cs" company="SemanticArchitecture">
|
||||
// http://www.SemanticArchitecture.net pkalkie@gmail.com
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// This class is responsible for rendering a HTML view to a string.
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator {
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Mvc.Html;
|
||||
|
||||
/// <summary>
|
||||
/// This class is responsible for rendering a HTML view into a string.
|
||||
/// </summary>
|
||||
public class HtmlViewRenderer {
|
||||
public string RenderViewToString(Controller controller, string viewName, object viewData) {
|
||||
var renderedView = new StringBuilder();
|
||||
using (var responseWriter = new StringWriter(renderedView)) {
|
||||
var fakeResponse = new HttpResponse(responseWriter);
|
||||
var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
|
||||
var fakeControllerContext = new ControllerContext(new HttpContextWrapper(fakeContext), controller.ControllerContext.RouteData, controller.ControllerContext.Controller);
|
||||
|
||||
var oldContext = HttpContext.Current;
|
||||
HttpContext.Current = fakeContext;
|
||||
|
||||
using (var viewPage = new ViewPage()) {
|
||||
var html = new HtmlHelper(CreateViewContext(responseWriter, fakeControllerContext), viewPage);
|
||||
html.RenderPartial(viewName, viewData);
|
||||
HttpContext.Current = oldContext;
|
||||
}
|
||||
}
|
||||
|
||||
return renderedView.ToString();
|
||||
}
|
||||
|
||||
private static ViewContext CreateViewContext(TextWriter responseWriter, ControllerContext fakeControllerContext) {
|
||||
return new ViewContext(fakeControllerContext, new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), responseWriter);
|
||||
}
|
||||
private static ViewContext CreateViewContext(TextWriter responseWriter, ControllerContext fakeControllerContext) {
|
||||
return new ViewContext(fakeControllerContext, new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), responseWriter);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
}
|
@ -1,54 +1,56 @@
|
||||
#if !NET8
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Security;
|
||||
#endif
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="PdfViewController.cs" company="SemanticArchitecture">
|
||||
// http://www.SemanticArchitecture.net pkalkie@gmail.com
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Extends the controller with functionality for rendering PDF views
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
#if !NET8
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator {
|
||||
using System.Web.Mvc;
|
||||
using System.IO;
|
||||
namespace Fab2ApprovalSystem.PdfGenerator;
|
||||
|
||||
/// <summary>
|
||||
/// Extends the controller with functionality for rendering PDF views
|
||||
/// </summary>
|
||||
public class PdfViewController : Controller {
|
||||
private readonly HtmlViewRenderer htmlViewRenderer;
|
||||
private readonly StandardPdfRenderer standardPdfRenderer;
|
||||
#if !NET8
|
||||
|
||||
public PdfViewController() {
|
||||
this.htmlViewRenderer = new HtmlViewRenderer();
|
||||
this.standardPdfRenderer = new StandardPdfRenderer();
|
||||
}
|
||||
public class PdfViewController : Controller {
|
||||
|
||||
protected ActionResult ViewPdf(string pageTitle, string viewName, object model) {
|
||||
// Render the view html to a string.
|
||||
string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);
|
||||
private readonly HtmlViewRenderer htmlViewRenderer;
|
||||
private readonly StandardPdfRenderer standardPdfRenderer;
|
||||
|
||||
// Let the html be rendered into a PDF document through iTextSharp.
|
||||
byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle);
|
||||
public PdfViewController() {
|
||||
htmlViewRenderer = new HtmlViewRenderer();
|
||||
standardPdfRenderer = new StandardPdfRenderer();
|
||||
}
|
||||
|
||||
// Return the PDF as a binary stream to the client.
|
||||
return new BinaryContentResult(buffer, "application/pdf");
|
||||
}
|
||||
protected ActionResult ViewPdf(string pageTitle, string viewName, object model) {
|
||||
// Render the view html to a string.
|
||||
string htmlText = htmlViewRenderer.RenderViewToString(this, viewName, model);
|
||||
|
||||
protected void SavePdf(string fileName, string viewName, object model) {
|
||||
// Render the view html to a string.
|
||||
string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);
|
||||
// Let the html be rendered into a PDF document through iTextSharp.
|
||||
byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle);
|
||||
|
||||
// Let the html be rendered into a PDF document through iTextSharp.
|
||||
byte[] buffer = standardPdfRenderer.Render(htmlText, "");
|
||||
// Return the PDF as a binary stream to the client.
|
||||
return new BinaryContentResult(buffer, "application/pdf");
|
||||
}
|
||||
|
||||
using (FileStream fs = new FileStream(fileName, FileMode.Create)) {
|
||||
fs.Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
protected void SavePdf(string fileName, string viewName, object model) {
|
||||
// Render the view html to a string.
|
||||
string htmlText = htmlViewRenderer.RenderViewToString(this, viewName, model);
|
||||
|
||||
// Let the html be rendered into a PDF document through iTextSharp.
|
||||
byte[] buffer = standardPdfRenderer.Render(htmlText, "");
|
||||
|
||||
using (FileStream fs = new(fileName, FileMode.Create)) {
|
||||
fs.Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -1,79 +1,102 @@
|
||||
#if !NET8
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Security;
|
||||
#endif
|
||||
|
||||
#if !NET8
|
||||
namespace Fab2ApprovalSystem.PdfGenerator {
|
||||
using iTextSharp.text;
|
||||
using iTextSharp.text.pdf;
|
||||
using iTextSharp.text;
|
||||
using iTextSharp.text.pdf;
|
||||
#endif
|
||||
|
||||
using System;
|
||||
#if !NET8
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// This class represents the standard header and footer for a PDF print.
|
||||
/// application.
|
||||
/// </summary>
|
||||
public class PrintHeaderFooter : PdfPageEventHelper {
|
||||
private PdfContentByte pdfContent;
|
||||
private PdfTemplate pageNumberTemplate;
|
||||
private BaseFont baseFont;
|
||||
private DateTime printTime;
|
||||
namespace Fab2ApprovalSystem.PdfGenerator;
|
||||
|
||||
public string Title { get; set; }
|
||||
#if !NET8
|
||||
|
||||
public override void OnOpenDocument(PdfWriter writer, Document document) {
|
||||
printTime = DateTime.Now;
|
||||
baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
|
||||
pdfContent = writer.DirectContent;
|
||||
pageNumberTemplate = pdfContent.CreateTemplate(50, 50);
|
||||
}
|
||||
public class PrintHeaderFooter : PdfPageEventHelper {
|
||||
|
||||
public override void OnStartPage(PdfWriter writer, Document document) {
|
||||
base.OnStartPage(writer, document);
|
||||
private PdfContentByte pdfContent;
|
||||
private PdfTemplate pageNumberTemplate;
|
||||
private BaseFont baseFont;
|
||||
private DateTime printTime;
|
||||
#endif
|
||||
|
||||
Rectangle pageSize = document.PageSize;
|
||||
#if NET8
|
||||
|
||||
if (Title != string.Empty) {
|
||||
pdfContent.BeginText();
|
||||
pdfContent.SetFontAndSize(baseFont, 11);
|
||||
pdfContent.SetRGBColorFill(0, 0, 0);
|
||||
pdfContent.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40));
|
||||
pdfContent.ShowText(Title);
|
||||
pdfContent.EndText();
|
||||
}
|
||||
}
|
||||
public class PrintHeaderFooter {
|
||||
|
||||
public override void OnEndPage(PdfWriter writer, Document document) {
|
||||
base.OnEndPage(writer, document);
|
||||
#endif
|
||||
|
||||
int pageN = writer.PageNumber;
|
||||
string text = pageN + " - ";
|
||||
float len = baseFont.GetWidthPoint(text, 8);
|
||||
public string Title { get; set; }
|
||||
|
||||
Rectangle pageSize = document.PageSize;
|
||||
pdfContent = writer.DirectContent;
|
||||
pdfContent.SetRGBColorFill(100, 100, 100);
|
||||
#if !NET8
|
||||
|
||||
public override void OnOpenDocument(PdfWriter writer, Document document) {
|
||||
printTime = DateTime.Now;
|
||||
baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
|
||||
pdfContent = writer.DirectContent;
|
||||
pageNumberTemplate = pdfContent.CreateTemplate(50, 50);
|
||||
}
|
||||
|
||||
public override void OnStartPage(PdfWriter writer, Document document) {
|
||||
base.OnStartPage(writer, document);
|
||||
|
||||
Rectangle pageSize = document.PageSize;
|
||||
|
||||
if (Title != string.Empty) {
|
||||
pdfContent.BeginText();
|
||||
pdfContent.SetFontAndSize(baseFont, 8);
|
||||
pdfContent.SetTextMatrix(pageSize.Width / 2, pageSize.GetBottom(30));
|
||||
pdfContent.ShowText(text);
|
||||
pdfContent.SetFontAndSize(baseFont, 11);
|
||||
pdfContent.SetRGBColorFill(0, 0, 0);
|
||||
pdfContent.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40));
|
||||
pdfContent.ShowText(Title);
|
||||
pdfContent.EndText();
|
||||
|
||||
pdfContent.AddTemplate(pageNumberTemplate, (pageSize.Width / 2) + len, pageSize.GetBottom(30));
|
||||
|
||||
pdfContent.BeginText();
|
||||
pdfContent.SetFontAndSize(baseFont, 8);
|
||||
pdfContent.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, printTime.ToString(), pageSize.GetRight(40), pageSize.GetBottom(30), 0);
|
||||
pdfContent.EndText();
|
||||
}
|
||||
|
||||
public override void OnCloseDocument(PdfWriter writer, Document document) {
|
||||
base.OnCloseDocument(writer, document);
|
||||
|
||||
pageNumberTemplate.BeginText();
|
||||
pageNumberTemplate.SetFontAndSize(baseFont, 8);
|
||||
pageNumberTemplate.SetTextMatrix(0, 0);
|
||||
pageNumberTemplate.ShowText(string.Empty + (writer.PageNumber - 1));
|
||||
pageNumberTemplate.EndText();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void OnEndPage(PdfWriter writer, Document document) {
|
||||
base.OnEndPage(writer, document);
|
||||
|
||||
int pageN = writer.PageNumber;
|
||||
string text = pageN + " - ";
|
||||
float len = baseFont.GetWidthPoint(text, 8);
|
||||
|
||||
Rectangle pageSize = document.PageSize;
|
||||
pdfContent = writer.DirectContent;
|
||||
pdfContent.SetRGBColorFill(100, 100, 100);
|
||||
|
||||
pdfContent.BeginText();
|
||||
pdfContent.SetFontAndSize(baseFont, 8);
|
||||
pdfContent.SetTextMatrix(pageSize.Width / 2, pageSize.GetBottom(30));
|
||||
pdfContent.ShowText(text);
|
||||
pdfContent.EndText();
|
||||
|
||||
pdfContent.AddTemplate(pageNumberTemplate, (pageSize.Width / 2) + len, pageSize.GetBottom(30));
|
||||
|
||||
pdfContent.BeginText();
|
||||
pdfContent.SetFontAndSize(baseFont, 8);
|
||||
pdfContent.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, printTime.ToString(), pageSize.GetRight(40), pageSize.GetBottom(30), 0);
|
||||
pdfContent.EndText();
|
||||
}
|
||||
|
||||
public override void OnCloseDocument(PdfWriter writer, Document document) {
|
||||
base.OnCloseDocument(writer, document);
|
||||
|
||||
pageNumberTemplate.BeginText();
|
||||
pageNumberTemplate.SetFontAndSize(baseFont, 8);
|
||||
pageNumberTemplate.SetTextMatrix(0, 0);
|
||||
pageNumberTemplate.ShowText(string.Empty + (writer.PageNumber - 1));
|
||||
pageNumberTemplate.EndText();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
@ -8,9 +8,6 @@ using System.IO;
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator;
|
||||
|
||||
/// <summary>
|
||||
/// This class is responsible for rendering a html text string to a PDF document using the html renderer of iTextSharp.
|
||||
/// </summary>
|
||||
public class StandardPdfRenderer {
|
||||
private const int HorizontalMargin = 40;
|
||||
private const int VerticalMargin = 40;
|
||||
|
Reference in New Issue
Block a user