I have an ASP.NET Core Web 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 endpoint is defined 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 an ASP.NET MVC (.NET 4.5) app. The following code works fine when the API was also .NET 4.5. The API is on another server. The client accesses the API over the network.
The client code is shown here:
string url = Constants.GetApiUrl() + "/mycontroller/" + 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
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
}
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 an ASP.NET Core 8.0 Web API for uploading an image and a text string.