Return an image from asp.net web api core as IActionResult - asp.net-core

What is the best way to return an image file as IActionResult while using asp.net web api core?
I tried returning a base64 string and it works fine. But not considered as efficient.
Is there a way using which we can return an image file object itself as IActionResult.

You can use the various overloads of the File() function in controllers that inherit from Controller or ControllerBase.
For example, you can do:
return File("~/Images/photo.jpg", "image/jpeg");
This uses a virtual path, other options include giving it a byte array or a Stream. You can also give a download file name as a third argument if that is needed.

[Route("getProductImage/v1")]
[HttpGet]
public async Task<IActionResult> getProductImage(GetProductImageQueryParam parammodel)
{
using (HttpClient client = new HttpClient())
{
MNimg_URL = MNimg_URL + parammodel.modelname;
HttpResponseMessage response = await client.GetAsync(MNimg_URL);
byte[] content = await response.Content.ReadAsByteArrayAsync();
//return "data:image/png;base64," + Convert.ToBase64String(content);
return File(content, "image/png", parammodel.modelname);
}
}
In .net core web api you can use the above code
here GetProductImageQueryParam is a class with input parameters

A File result is called FileContentResult in NET Core 3.x.

You can return image using return file with stream or bytes format or using its image path.
There are few overloaded methods for return File(//parameters); which you can use it in mvc controller's action method.
API Controller
[Route("api/[controller]")]
public class FileController : Controller {
//GET api/file/id
[HttpGet("{id}"]
public async Task<IActionResult> GetFile(string id) {
var stream = await {{//__get_stream_here__//}};
var response = File(stream, "application/octet-stream"); // FileStreamResult
return response;
}
}
or
var imageFileStream = System.IO.File.OpenRead("// image path");
return File(imageFileStream, "image/jpeg");
Hope this will help you.

Related

How return a yaml file as result of an asp.net core ViewComponent

I want to create an asp.net core ViewComponent that dynamically return a yaml file based on some criteria:
For example
namespace MyNameSpace {
[ViewComponent(Name = nameof(MyViewComponent))]
public class MyViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(object input)
{
string yamlDocument = GetYamlDocumentByInput(input);
//how to proceed here so that my yamlDocument is returned with the right content type?
return View(..., yamlDocument);
}
}}
you could search the view component class,and there‘s no method can return a file as result.
you'd better add an action in your controller to download file,and you could send a request to this action after your view has been rendered mannully or automaticlly.
and there's the codes in the action:
public FileResult DownLoad(Person person)
{
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(person);
byte[] yamlArray = System.Text.Encoding.UTF8.GetBytes(yaml);
return File(yamlArray, "application/x-yml");
}
Result:

HttpClient.GetAsync return HttpResponseMessage with null header

net 5.0 lover.
I am new in blazor and .net 5.0, I develop the application with blazor WebAssembly and WebApi.
There are two major Projects: Client, Server.
Client is Blazor WebAssembly and Server is WebApi Controller Project.
In server side, in controller, HttpGet Method, i add a value to Response header:
[HttpGet]
public async Task<ActionResult<IList<Country>>> GetAsync([FromQuery] Pagination paginationDto)
{
/...
httpContext.Response.Headers.Add("TotalPages", totalPages.ToString());
//...
IList<Country> = ...
return result;
}
In Client project razor page, call the api with following method from generic calss:
protected virtual async Task<PaginatedResponse<O>> GetAsync<O>(Pagination pagination)
{
HttpResponseMessage response = null;
try
{
response = await httpClient.GetAsync(RequestUri);
if (response.IsSuccessStatusCode)
{
try
{
//This response Header always is null!
System.Console.WriteLine("response.Headers: " + response.Headers.ToString());
O result = await response.Content.ReadFromJsonAsync<O>();
var paginatedResponse = new PaginatedResponse<O>
{
Response = result,
TotalPages = totalPages
};
return paginatedResponse;
}
//...
return default;
}
When Api call from postman the result and Header is fine and TotalPages is there.
In Client App, the result is ok, but the Header is null.
Any information will save me ;-)
Thanks in Advance.
I think you're overcomplicating this by trying to use headers to pass back a result that can be passed more easily as part of the content. You even sort of realise this you're trying to use a PaginatedResponse in the Blazor client.
So instead of the API returning just a list, have a PaginatedResponse class in a shared library somewhere.. e.g.
/// <summary>
/// Paged result class
/// </summary>
/// <typeparam name="T"></typeparam>
public class PaginatedResponse<T>
{
public int TotalPages { get; set; }
public int Page { get; set; }
public List<T> Data { get; set; }
}
Your API then returns this
[HttpGet]
public async Task<ActionResult<PaginatedResponse<Country>>> GetAsync([FromQuery] Pagination paginationDto)
{
// ... query results here
var result = new PaginatedResponse<Country>()
{
Page = x,
TotalPages = totalPages,
Data = countrylist // from query
};
return result;
}
Your Blazor client can then use the same PaginatedResponse class and just use the standard GetFromJsonAsync method:
var result = await Http.GetFromJsonAsync<PaginatedResponse<Country>>("yourApiUri");
This is why I love Blazor!
This is the exactly answer for how search for answer:
in Server project, in startup.cs, in ConfigureServices method, add following code for CORS or update your CORS rule:
services.AddCors(options => options.AddPolicy(name: "WebApiProjectName or somthing", builder =>
{
builder.WithOrigins("http://localhost:xxxx") //xxxxx is server port
.AllowAnyMethod()
.AllowAnyHeader()
//.AllowCredentials() // its optional for this answer
.WithExposedHeaders("*"); // this is the code you need!
}));

Consume API into aspnet core 2.2

I have a web API which returns about 1000 records into json. Now I want to get all those records from json response and deserialize into a model
The code i have written so far is like following:
public async Task<IActionResult> GetList()
{
Facility facilityInfo = new Facility();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("http://localhost:55555/api/Facilities"))
{
string apiResponse = await response.Content.ReadAsStringAsync();
facilityInfo = JsonConvert.DeserializeObject<Facility>(apiResponse);
}
}
//-----------other parts of code-----------------
return View();
}
The problem is that it returns null facilityInfo
Thank you
You're deserializing something that has a property called items which is a list of Facilities. So if you have the following structure:
public class Facilities
{
public List<Facility> items;
}
And then
Facilities facilities = JsonConvert.DeserializeObject<Facilities>(apiResponse);
The facilities.items property will contain the data.

How to use ReadAsStringAsync in asp.net core MVC controller?

How to use ReadAsStringAsync in asp.net core MVC controller?
The Microsoft.AspNetCore.Mvc.Request does not have Content property. Is there an alternative to this? Thank you!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using AuthLibrary;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using System.Threading.Tasks;
[Microsoft.AspNetCore.Mvc.Route("TestAPI")]
public class TestController : Controller
{
[Microsoft.AspNetCore.Mvc.HttpPost]
[AllowAnonymous]
[Microsoft.AspNetCore.Mvc.Route("Start")]
public async Task<HttpResponseMessage> Start()
{
string req = await this.Request.Content.ReadAsStringAsync();
////
}
}
For Asp.Net Core MVC, you could access the request content with request.Body.
Here is an extension:
public static class HttpRequestExtensions
{
/// <summary>
/// Retrieve the raw body as a string from the Request.Body stream
/// </summary>
/// <param name="request">Request instance to apply to</param>
/// <param name="encoding">Optional - Encoding, defaults to UTF8</param>
/// <returns></returns>
public static async Task<string> GetRawBodyStringAsync(this HttpRequest request, Encoding encoding = null)
{
if (encoding == null)
encoding = Encoding.UTF8;
using (StreamReader reader = new StreamReader(request.Body, encoding))
return await reader.ReadToEndAsync();
}
/// <summary>
/// Retrieves the raw body as a byte array from the Request.Body stream
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static async Task<byte[]> GetRawBodyBytesAsync(this HttpRequest request)
{
using (var ms = new MemoryStream(2048))
{
await request.Body.CopyToAsync(ms);
return ms.ToArray();
}
}
}
Use:
public async Task<string> ReadStringDataManual()
{
return await Request.GetRawBodyStringAsync();
}
Reference:Accepting Raw Request Body Content in ASP.NET Core API Controllers
You hope you can use .ReadAsStringAsync() on the current MVC request because perhaps you've seen something like this?
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Threading.Tasks;
namespace DL.SO.UI.Web.Controllers
{
public class DashboardController : Controller
{
// In order to be able to inject the factory, you need to register in Startup.cs
// services.AddHttpClient()
// .AddRouting(...)
// .AddMvc(...);
private readonly IHttpClientFactory _httpClientFactory;
public DashboardController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<IActionResult> Index()
{
var client = _httpClientFactory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://www.google.com");
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string bodyContent = await response.Content.ReadAsStringAsync();
}
return View();
}
}
}
This is how you use HttpClient to fetch external data/resources in an app. .ReadAsStringAsync() is off an HttpContent, which is the property of either HttpRequestMessage or HttpResponseMessage. Both HttpRequestMessage and HttpResponseMessage are in System.Net.Http namespace.
But now you're in the app itself! Things work a little bit differently. We don't have a response for the request yet (because we haven't done return View();). Hence I assume the content you want to look at is the content of the request coming in?
GET request's content
When a GET request comes in, MVC will automatically bind request's query strings to action method parameters in the controller. They're also available in the Query property off the current Request object:
public IActionResult Index(int page = 1, int size = 15)
{
foreach (var param in Request.Query)
{
...
}
return View();
}
POST request's content
When a POST request comes in, Request.Body might not always have the data you're looking for. It depends on the content type of the POST request.
By default when you're submitting a form, the content type of the request is form-data. MVC then will bind the inputs to your view model as the action parameter:
[HttpPost]
public async Task<IActionResult> Close(CloseReservationViewModel model)
{
Request.Form // contains all the inputs, name/value pairs
Request.Body // will be empty!
...
}
If you use jQuery to fire POST requests without specifying the contentType, it defaults to x-www-form-urlencoded:
#section scripts {
<script type="text/javascript">
$(function() {
$.ajax({
url: '#Url.Action("test", "dashboard", new { area = "" })',
data: {
name: 'David Liang',
location: 'Portland Oregon'
},
method: 'POST'
}).done(function (response) {
console.info(response);
});
});
</script>
}
[HttpPost]
public async Task<IActionResult> Test()
{
string body;
using (var reader = new StreamReader(Request.Body))
{
body = await reader.ReadToEndAsync();
}
return Json(body);
}
Conclusion
If you want to use HttpClient to call external services inside your MVC app, you can utilize IHttpClientFactory, HttpClient from System.Net.Http and get a HttpContent from either the request or response without too much trouble. Then you can do ReadAsStringAsync() off it.
If you want to peek on the request data sent from the client to your MVC app, MVC has already done so much to help you bind the data using model binding. You can also read request's body for POST requests with a StreamReader. Just pay attention that depends on the content type, Request.Body might not have what you expect.

Return Image using IHttpActionResult

I have a requirement to return an image from a WebApi using IHttpActionResult instead of HttpResponseMessage. How this can be achieved & what are the accept & content type headers need to be passed?
Thanks!!
I was actually looking for a solution to send an image resides on internet from web api but I am returning local thumbnail images from my web api with the following code;
[HttpGet]
[Route("thumbnail/{userId}/{fileName}")]
public IHttpActionResult GetThumbnail()
{
var mediaRoot = System.Web.HttpContext.Current.Server.MapPath("~/media");
var imgPath = Path.Combine(mediaRoot, "images", userId, "thumbnail", fileName);
var fileInfo = new FileInfo(imgPath);
return !fileInfo.Exists
? (IHttpActionResult)NotFound()
: new FileResult(fileInfo.FullName);
}
Hope this helps
Edit: You should create a custom IHttpActionResult to retun images,
It seems i have created a custom IHttpActionResult to return images and here is the post that i got this code from > Custom IHttpActionResult
This would be a basic way:
[HttpGet]
[Route("test")]
public IHttpActionResult GetImageTest()
{
var stream = File.OpenRead(#"C:\picture.jpg");
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
response.Content.Headers.ContentLength = stream.Length;
return ResponseMessage(response);
}