Refactor PDF viewing and integrate DevExpress support
- Removed folder reference for "Controllers" in the project file. - Updated `ViewPdf.cshtml` to include layout and clarify PDF output. - Enhanced `ViewPdf.cshtml.cs` with improved logging and error handling. - Added MVC controller services in `Program.cs` for DevExpress functionality. - Introduced `DocumentViewerController` to manage document viewing requests.
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using DevExpress.AspNetCore.Reporting.WebDocumentViewer;
|
||||||
|
using DevExpress.AspNetCore.Reporting.WebDocumentViewer.Native.Services;
|
||||||
|
|
||||||
|
namespace DXApp.TemplateKitProject.Controllers
|
||||||
|
{
|
||||||
|
[Route("DocumentViewer")]
|
||||||
|
public class DocumentViewerController : WebDocumentViewerController
|
||||||
|
{
|
||||||
|
// Hier den erwarteten DevExpress-Service injizieren und an die Basisklasse weitergeben
|
||||||
|
public DocumentViewerController(IWebDocumentViewerMvcControllerService controllerService)
|
||||||
|
: base(controllerService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,8 +29,4 @@
|
|||||||
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="3.0.71" />
|
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="3.0.71" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Controllers\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,2 +1,6 @@
|
|||||||
@page
|
@page
|
||||||
@model DXApp.TemplateKitProject.Pages.Invoices.ViewPdfModel
|
@model DXApp.TemplateKitProject.Pages.Invoices.ViewPdfModel
|
||||||
|
@{
|
||||||
|
Layout = "_Layout";
|
||||||
|
// Diese Seite liefert keinen HTML-Content; der Handler gibt die PDF zurück.
|
||||||
|
}
|
||||||
@@ -1,24 +1,40 @@
|
|||||||
using DXApp.TemplateKitProject.Data;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
using DXApp.TemplateKitProject.Data;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace DXApp.TemplateKitProject.Pages.Invoices;
|
namespace DXApp.TemplateKitProject.Pages.Invoices;
|
||||||
|
|
||||||
public class ViewPdfModel(AppDbContext db) : PageModel
|
public class ViewPdfModel : PageModel
|
||||||
{
|
{
|
||||||
|
private readonly AppDbContext _db;
|
||||||
|
private readonly ILogger<ViewPdfModel> _logger;
|
||||||
|
|
||||||
|
public ViewPdfModel(AppDbContext db, ILogger<ViewPdfModel> logger)
|
||||||
|
{
|
||||||
|
_db = db;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> OnGetAsync(int id)
|
public async Task<IActionResult> OnGetAsync(int id)
|
||||||
{
|
{
|
||||||
var invoice = await db.ZugferdInvoices
|
// Sicherheit: defensive checks, gleiche Logik wie CustomReportStorageWebExtension
|
||||||
|
var invoice = await _db.ZugferdInvoices
|
||||||
|
.AsNoTracking()
|
||||||
.FirstOrDefaultAsync(i => i.Id == id);
|
.FirstOrDefaultAsync(i => i.Id == id);
|
||||||
|
|
||||||
if (invoice is null || string.IsNullOrEmpty(invoice.ResultFilePath))
|
if (invoice is null)
|
||||||
|
return NotFound();
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(invoice.ResultFilePath))
|
||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
||||||
if (!System.IO.File.Exists(invoice.ResultFilePath))
|
if (!System.IO.File.Exists(invoice.ResultFilePath))
|
||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
||||||
var bytes = await System.IO.File.ReadAllBytesAsync(invoice.ResultFilePath);
|
var bytes = await System.IO.File.ReadAllBytesAsync(invoice.ResultFilePath);
|
||||||
return File(bytes, "application/pdf");
|
_logger.LogInformation("ViewPdf: Invoice {Id} ausgeliefert ({Size} Bytes).", id, bytes.Length);
|
||||||
|
|
||||||
|
return File(bytes, "application/pdf", $"{Path.GetFileName(invoice.ResultFilePath)}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,9 @@ using DevExpress.XtraReports.Web.Extensions;
|
|||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// MVC-Controller hinzufügen (benötigt für DevExpress WebDocumentViewer)
|
||||||
|
builder.Services.AddControllersWithViews();
|
||||||
|
|
||||||
builder.Services.AddRazorPages();
|
builder.Services.AddRazorPages();
|
||||||
|
|
||||||
// DevExpress Controls und Reporting Services
|
// DevExpress Controls und Reporting Services
|
||||||
@@ -52,5 +55,8 @@ app.UseDevExpressControls();
|
|||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
// Controller-Routen verfügbar machen (wichtig für DevExpress WebDocumentViewer)
|
||||||
|
app.MapControllers();
|
||||||
app.MapRazorPages();
|
app.MapRazorPages();
|
||||||
app.Run();
|
app.Run();
|
||||||
Reference in New Issue
Block a user