ASP.NET CORE 8 FileStreamResult Senden nicht BytesC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 ASP.NET CORE 8 FileStreamResult Senden nicht Bytes

Post by Anonymous »

Mein Problem begann gestern, als ich versuchte, eine ASP.NET Core 8 -Anwendung zu erhalten, um eine Datei an den Client zurückzuschicken (über eine Aufgabe < /code> Methode, die einen fileStreamResult < /code>) zurückgab und einen sehr interessanten Fehler: < /p>

Verbindungs-ID "0HNCVO92UT954", Anfrage ID "0HNCVO92UT954: 00000003":

Eine ungehandelte Ausnahme wurde von der Anwendung ausgelöst. /> < /blockquote>
Ich sehe ein Problem, bei dem überhaupt keine Bytes gesendet werden, nicht nur ein Missverhältnis.

Code: Select all

public class MainController : Controller
{
private readonly ILogger _logger;

public MainController(ILogger logger)
{
_logger = logger;
}

[HttpGet]
[Route("api/{unitControlNumber}/{startDate}/{endDate}")]
public async Task RecordsExport(Guid? unitControlNumber, DateTime? startDate, DateTime? endDate)
{
// create a memory stream and fill it with some random data
Stream stream = new MemoryStream();
byte[] thingBytes = {1,2,3,4,5,6,7,8};
stream.Write(thingBytes, 0, thingBytes.Length);

// I saw a question once where someone recommended to do this but I'm not sure if this is necessary or even helpful (I know it dumps the buffer contents into the actual stream, I'm just not aware of what is contained in the buffers or why there would be data left there to dump in the first place (dump most likely being incorrect terminology))
stream.Flush();

// - - send the excel file (now just some bytes)
// I've tried using "application/txt" as well for the file format but it did not work either
FileStreamResult file = new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
file.FileDownloadName = "Board_Data_For_Unit-" + unitControlNumber + "__Start-" + startDate + "__End-" + endDate;
file.EnableRangeProcessing = false;
Console.WriteLine("Can seek? " + stream.CanSeek);
Console.WriteLine(stream.ToString());

return file;
}
}
Wie ist mein Programm.using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using VPN_Website_New.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

// Build the application!
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{action=Index}/{id?}");

app.Run();
< /code>
Wenn ich die Website mit Haltepunkten ausführe, kann ich sehen, dass der Speicherstrom im obigen Code tatsächlich mit den erforderlichen Daten gefüllt ist (8 Bytes von Daten). Aus meinen Annahmen könnte es sich um den ASP.NET -Quellcode handeln (der nach dem Durchsehen gut aussieht, so dass es nicht so sein sollte) oder etwas mit meinem Programm zu tun hat. Es könnte auch ein eindeutiger Fehler sein, den diese spezifische Maschine und diese ASP.NET -Version erlebt. Apropos, meine ASP.NET -Core -Version ist 8.0.16, ich führe diese derzeit von Visual Studio 2022 IIS aus und ich bin unter Windows 11.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post