I have a .net core Api to upload images to a gallery. It needs image upload and a user string at the end point. In swagger it works fine and shows a button for image upload and a text box to enter the user name.
The Api end point is like this
[HttpPost]
public List<Models.Image> PostUploadImage(IFormFile file, string user)
{
if (_utility.IsValidImageFile(file))
{
string currentRootPath = _hostEnvironment.ContentRootPath;
List<Models.Image> images = _utility.ObtainUploadedImages(file, user, currentRootPath);
return images;
}
else
{
string error = $"{file} is not a valid image";
var image = new Models.Image { Error = error };
return [image];
}
}
The api works fine using swagger. My client is still .Net MVC (4.5). The following code works fine when the API was also .net 4.5. The API is in another server. The client access the API over the network.
The client code is shown below
string url = Constants.GetGalleryApiUrl() + "/gallery/" + user;
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.PostAsync(url, Request.Content);
List<Models.Image> images = new List<Models.Image>();
if (response.IsSuccessStatusCode)
{
images = JsonToModel(response.Content);
return images;
}
else
{
throw new HttpResponseException(response.StatusCode);
}
}
Right now it breaks in the client with 405 error
response {StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Sun, 19 May 2024 16:00:37 GMT
Server: Kestrel
Content-Length: 0
Allow: GET
}} System.Net.Http.HttpResponseMessage
My question is how do I set up my URL and upload the image.
I tried to search all over the internet but only found how to upload images using .net core.
I would like to know how to set up a client using .net 4.5 to access a .net core api 8.0 for uploading an image and a text string.