简体   繁体   English

在 ASP.NET MVC 视图中显示来自 .NET 核心 API 的图像是否正确?

[英]Is it correct way of displaying Image from .NET Core API in ASP.NET MVC View?

I have a .NET Core API that returns an image:我有一个返回图像的 .NET Core API:

 [HttpGet]
 public IActionResult GetImage()
 {
     return File("Image bytes", "image/jpeg");
 }

Controller: Controller:

public async Task<IActionResult> Index()
{
    using var client = new HttpClient();

    var response = await client.GetAsync("uri");

    var bytes = await response.Content.ReadAsByteArrayAsync();
    var base64 = Convert.ToBase64String(bytes);
    var imgSrc = string.Format("data:image/jpeg;base64,{0}", base64);            

    var model = new ImageData { ImageSrc = imgSrc };

    return View(model);
}

And in the view, I just pass the image source to img element.在视图中,我只是将图像源传递给img元素。

Everything works fine, but is it the correct way of doing it?一切正常,但这是正确的做法吗?

The API returns the response that has FileContentResult . API 返回具有FileContentResult的响应。 Is there a way to render that result without converting it to Base64 string and do what I am doing?有没有办法在不将其转换为 Base64 字符串的情况下呈现该结果并执行我正在做的事情?

Appreciate any comments.感谢任何评论。

Your API code is simply returning an image as any other web server would (from the client's perspective), so unless there's some authentication step that you haven't shown, you should be able to simply use the endpoint URL directly in an img tag.您的 API 代码只是返回图像,就像任何其他 web 服务器一样(从客户端的角度来看),所以除非有一些您没有显示的身份验证步骤,否则您应该能够直接使用端点img和 im8B4D45903DZ

Take your "uri" value and insert it into an image tag on your rendered page:获取您的"uri"值并将其插入到渲染页面上的图像标签中:

<img src="uri">

For example, if your uri is https://www.example.com/controller/getImage then you would end up with:例如,如果您的 uri 是https://www.example.com/controller/getImage那么您最终会得到:

<img src="https://www.example.com/controller/getImage">

Then the browser will directly connect to your API server to pull the image.然后浏览器会直接连接到你的API服务器来拉取图片。

Incidentally, if you're just trying to serve local files from your ASP.NET Core API, you might be interested in using the built-in static file functionality instead:顺便说一句,如果您只是想从 ASP.NET 内核 API 提供本地文件,您可能会对使用内置的static 文件功能感兴趣:

Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients by default. Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients by default.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC视图未显示数据库中的正确信息 - ASP.NET MVC view not displaying correct info from database 在 ASP.Net Core 2 MVC 中禁用 model 验证的正确方法 - Correct way to disable model validation in ASP.Net Core 2 MVC ASP.NET MVC 5 Web API不接受来自MVC ASP.NET Core解决方案的POST上的视图模型 - ASP.NET MVC 5 Web API not accepting View Model on POST from MVC ASP.NET Core solution 在asp.net core 2 MVC中通过ajax在视图中显示来自控制器的json字符串 - displaying json strings from controller in view over ajax in asp.net core 2 MVC ASP.NET Core MVC视图显示列表中输入的错误值 - ASP.NET Core MVC view displaying incorrect values from input in list 在ASP.NET MVC视图中显示数据库中的图像 - Displaying images from database in an ASP.NET MVC View ASP.NET MVC:从数据库加载图像并在视图中显示它们 - ASP.NET MVC: loading images from database and displaying their in view ASP.NET MVC 5如何防止View显示为文本 - ASP.NET MVC 5 How to prevent View from displaying as text 如何将ASP.NET MVC核心视图和ASP.NET WEB API控制器分离到单独的项目中? - How to seperate ASP.NET MVC core view and ASP.NET WEB API controllers into separate projects? 在Razor / MVC5(Asp.net)中显示来自db的图像 - Displaying image from db in Razor/MVC5 (Asp.net)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM