使用来自 asp.net 核心 mvc 控制器的 asp.net 核心网络 api 进行文件上传
consuming asp.net core web api from asp.net core mvc controller for file upload
From swagger screenshot当我尝试从 swagger 或 postman 中 post 文件时,它工作正常。当我试图从 mvc web 应用程序调用 api 时,它不是 working.I 是 asp.net 核心的新手,没有显示错误,但 api 控制器没有点击这里是mvc控制器代码
public async Task<PostViewModel> AddPost(PostViewModel model)
{
TestViewModel testViewModel = new TestViewModel()
{
Description = model.Description,
Files = model.Files[0],
};
var formContent = new MultipartFormDataContent();
formContent.Add(new StringContent(testViewModel.Description), "Description");
formContent.Add(new StreamContent(testViewModel.Files.OpenReadStream()), "Files", Path.GetFileName(testViewModel.Files.FileName));
HttpClient hc = new HttpClient();
hc.BaseAddress = new Uri("baseurl");
var response = await hc.PostAsync("PostFile", formContent);
string apiResponse = await response.Content.ReadAsStringAsync();
return null;
//...
}`
我的api控制器
[HttpPost("PostFile", Name = "PostFile")]
public async Task<IActionResult> PostFile([FromForm] TestViewModel testViewModel)
{
Console.Write("Hello");
return null;
}
我的建议是:
webapi和mvc不在同一个项目,需要在mvc启动时设置cors:
public void ConfigureServices(IServiceCollection services)
{
......
services.AddCors(options =>
{
options.AddPolicy("any", builder =>
{
builder.WithOrigins("https://localhost:44320/WeatherForecast", "https://localhost:44393/WeatherForecast/PostFile")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors("cors");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
控制器中的代码:
public IActionResult AddPost(PostViewModel model)
{
TestViewModel testViewModel = new TestViewModel()
{
Description = model.Description,
File = model.File,
};
var url = "https://localhost:44320/WeatherForecast";
HttpClient hc = new HttpClient();
var formContent = new MultipartFormDataContent();
formContent.Add(new StringContent(testViewModel.Description), "Description");
formContent.Add(new StreamContent(testViewModel.File.OpenReadStream()), "File", Path.GetFileName(testViewModel.File.FileName));
var response = hc.PostAsync(url,formContent).Result;
return Ok();
}
From swagger screenshot当我尝试从 swagger 或 postman 中 post 文件时,它工作正常。当我试图从 mvc web 应用程序调用 api 时,它不是 working.I 是 asp.net 核心的新手,没有显示错误,但 api 控制器没有点击这里是mvc控制器代码
public async Task<PostViewModel> AddPost(PostViewModel model)
{
TestViewModel testViewModel = new TestViewModel()
{
Description = model.Description,
Files = model.Files[0],
};
var formContent = new MultipartFormDataContent();
formContent.Add(new StringContent(testViewModel.Description), "Description");
formContent.Add(new StreamContent(testViewModel.Files.OpenReadStream()), "Files", Path.GetFileName(testViewModel.Files.FileName));
HttpClient hc = new HttpClient();
hc.BaseAddress = new Uri("baseurl");
var response = await hc.PostAsync("PostFile", formContent);
string apiResponse = await response.Content.ReadAsStringAsync();
return null;
//...
}`
我的api控制器
[HttpPost("PostFile", Name = "PostFile")]
public async Task<IActionResult> PostFile([FromForm] TestViewModel testViewModel)
{
Console.Write("Hello");
return null;
}
我的建议是:
webapi和mvc不在同一个项目,需要在mvc启动时设置cors:
public void ConfigureServices(IServiceCollection services)
{
......
services.AddCors(options =>
{
options.AddPolicy("any", builder =>
{
builder.WithOrigins("https://localhost:44320/WeatherForecast", "https://localhost:44393/WeatherForecast/PostFile")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors("cors");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
控制器中的代码:
public IActionResult AddPost(PostViewModel model)
{
TestViewModel testViewModel = new TestViewModel()
{
Description = model.Description,
File = model.File,
};
var url = "https://localhost:44320/WeatherForecast";
HttpClient hc = new HttpClient();
var formContent = new MultipartFormDataContent();
formContent.Add(new StringContent(testViewModel.Description), "Description");
formContent.Add(new StreamContent(testViewModel.File.OpenReadStream()), "File", Path.GetFileName(testViewModel.File.FileName));
var response = hc.PostAsync(url,formContent).Result;
return Ok();
}