Create ASP.NET Core 3 Web API Project
On the Visual Studio, create new ASP.NET Core Web Application project
Input Project Name and select Project Location
Select Empty Template and click Create button to Finish
Structure of New Project
Add Configurations
Open Startup.cs file and add new configurations as below:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace LearnASPNETCore3WebAPIWithRealApps
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Add Image Folder
Create new folder named images in wwwroot folder
Create Controller
Create new folder named Controllers. In this folder, create new controller named FileController.cs as below:
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace LearnASPNETCoreWebAPIWithRealApps.Controllers
{
[Route("api/file")]
public class FileController : Controller
{
private IWebHostEnvironment iwebHostEnvironment;
public DemoController(IWebHostEnvironment _iwebHostEnvironment)
{
this.iwebHostEnvironment = _iwebHostEnvironment;
}
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile file)
{
try
{
var path = Path.Combine(this.iwebHostEnvironment.WebRootPath, "images", file.FileName);
var stream = new FileStream(path, FileMode.Create);
await file.CopyToAsync(stream);
return Ok(new { length = file.Length, name = file.FileName });
}
catch
{
return BadRequest();
}
}
}
}
Structure of ASP.NET Core 3 Web API Project
Testing Web API with PostMan
Consume Web API from Console Application
Create Console App (.NET Framework) Project in Visual Studio.
Entity Class
Create Models folder in Console Application. In this folder, create new class named FileResult.cs as below:
FileResult Entity
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnASPNETCore3WebAPIWithRealApps_Client
{
public class FileResult
{
public long Length { get; set; }
public string Name { get; set; }
}
}
Create UploadRestClientModel
UploadRestClientModel class contain methods call Web API. Add reference to System.Net.Http.Formatting library from Nuget Packages
using LearnASPNETCore3WebAPIWithRealApps_Client.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace LearnASPNETCore3WebAPIWithRealApps_Client
{
public class UploadRestClientModel
{
private string BASE_URL = "http://localhost:18942/api/file/";
public Task<HttpResponseMessage> Upload(FileInfo fileInfo)
{
try
{
var httpClient = new HttpClient();
var multipartFormDataContent = new MultipartFormDataContent();
httpClient.BaseAddress = new Uri(BASE_URL);
var fileContent = new ByteArrayContent(File.ReadAllBytes(fileInfo.FullName));
multipartFormDataContent.Add(fileContent, "file", fileInfo.Name);
return httpClient.PostAsync("upload", multipartFormDataContent);
}
catch
{
return null;
}
}
}
}
Run Console Application
using LearnASPNETCore3WebAPIWithRealApps_Client.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace LearnASPNETCore3WebAPIWithRealApps_Client
{
class Program
{
static void Main(string[] args)
{
try
{
var fileInfo = new FileInfo(@"E:\Photo\thumb2.gif");
var uploadRestClientModel = new UploadRestClientModel();
var result = uploadRestClientModel.Upload(fileInfo).Result;
var fileResult = result.Content.ReadAsAsync<FileResult>().Result;
Console.WriteLine("Status: " + result.StatusCode);
Console.WriteLine("File name: " + fileResult.Name);
Console.WriteLine("File size: " + fileResult.Length);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
}
Output
Status: OK
File name: thumb2.gif
File size: 8063