我正在我的控制器上構建一個方法來生成 SVG QR 碼 (QRCoder),并且我正在嘗試使用控制器上的方法將生成的 SVG 從控制器行內到視圖中。
當我嘗試直接查看該方法時,我看到 SVG XML 成功回傳,但出現例外。我不太確定我需要什么才能讓我的控制器回傳,以便我可以在我的視圖中執行此操作。
<img src="~/Redirect/QRCode/{code}/svg"/>
等效的 PNG 版本完美運行。
<img src="~/Redirect/QRCode/{code}/png"/>
[Route("[controller]/QRCode/{code}/{format?}")]
public IActionResult QRCodeImage(string code, BarcodeService.Format format = BarcodeService.Format.Png)
{
//Database call to get the real Uri...
var uri = "https://github.com/paulfarry/";
switch (format)
{
case BarcodeService.Format.Svg:
{
var data = barcodeService.GenerateQRCodeSvg(uri);
return File(data, "image/svg xml; charset=utf-8");
}
case BarcodeService.Format.Png:
default:
{
var data = barcodeService.GenerateQRCode(uri);
return File(data, "image/png");
}
}
}
當我直接查看頁面時,我希望看到呈現的 SVG,但我得到了這個例外資訊以及 SVG 資料。
https://localhost:5001/Redirect/QRCode/a/svg
An unhandled exception occurred while processing the request.
FileNotFoundException: Could not find file:
<svg version="1.1" baseProfile="full" shape-rendering="crispEdges" width="740" height="740" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect x="0" y="0" width="740" height="740" fill="#FFFFFF" />
<!--Remaining SVG data is here-->
</svg>
Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.ExecuteAsync(ActionContext context, VirtualFileResult result)
Microsoft.AspNetCore.Mvc.VirtualFileResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultAsync(IActionResult result)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResultFilterAsync<TFilter, TFilterAsync>()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
uj5u.com熱心網友回復:
事實證明解決起來非常簡單。只需要回傳Content而不是File
但是做了很多實驗。
case BarcodeService.Format.Svg:
{
var data = barcodeService.GenerateQRCodeSvg(uri);
return Content(data, "image/svg xml);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442054.html
上一篇:鏈接標簽不起作用:不可點擊的鏈接
下一篇:條件等于不作業時的Nlog過濾器
