Created
May 16, 2019 10:48
-
-
Save rosieks/c26c64ee1f0eb19efb07e5783909c20b to your computer and use it in GitHub Desktop.
ASP.NET Core handling multipart form data request issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
namespace aspnet_forddata_issue.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class ValuesController : ControllerBase | |
{ | |
// GET api/values | |
[HttpPost(Name = "upload")] | |
public ActionResult UploadFile([FromForm]IFormFile file) | |
{ | |
return Ok(); | |
} | |
[HttpGet] | |
public async Task<HttpResponseMessage> Test() | |
{ | |
string host = this.Url.ActionContext.HttpContext.Request.Host.Value; | |
string protocol = this.Url.ActionContext.HttpContext.Request.Scheme; | |
string url = this.Url.RouteUrl("upload", null, protocol, host); | |
var client = new HttpClient(); | |
var request = new HttpRequestMessage(HttpMethod.Post, url) | |
{ | |
Content = new MultipartFormDataContent(), | |
}; | |
request.Content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data"); | |
request.Content.Headers.ContentDisposition = null; | |
var response = await client.SendAsync(request); | |
return response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment