ファイルをアップロードしたいとしましょう。WebコントローラアクションからAPIコントローラアクションにファイル(IFormFile)を送信する方法?
レイザービューで私のHTMLフォームは次のようになります。私は、さらに使用しています
public async Task<IActionResult> Save(TenantBranding tenantBranding)
{
var result =
await _apiClient.PostAsync("/BrandingSettings", tenantBranding);
switch (result.Status)
{
case HttpStatusCode.NotFound:
case HttpStatusCode.BadRequest:
return new RedirectResult("~/Error/404");
case HttpStatusCode.OK:
//return View("Edit", result.Message.Content);
default:
return new RedirectResult("~/Error/500");
}
}
:
@model SequereMe.Onboarding.Web.ViewModels.Branding.TenantBrandingViewModel
@{
}
<form asp-controller="BrandingSettings" asp-action="Save" asp-method="post" enctype="multipart/form-data">
<div class="form-group">
<input asp-for="TenantId" [email protected] />
</div>
<div class="form-group">
<label for="logoFileUrl">Logo File Upload:</label>
<input asp-for="LogoFile" type="file" class="form-control-file" id="logoFileUrl" aria-describedby="fileHelp">
<h1>@Model.LogoFileUrl</h1>
</div>
<div class="form-group">
<label for="backgroundFileUrl">Background File Upload:</label>
<input asp-for="BackgroundFile" type="file" class="form-control-file" id="backgroundFileUrl" aria-describedby="fileHelp">
<h1>@Model.BackgroundFileUrl</h1>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
このHTMLフォームが提出にヒットするコントローラのアクションは、このウェブコントローラのアクションでありますFluentClientのPathoschild.Http.Client.IClientインターフェイスを使用して、このPostメソッドへのAPI呼び出しを呼び出します。
[HttpPost]
public async Task<IActionResult> Post([FromBody] TenantBranding tenantBranding)
{
if (tenantBranding == null)
{
return new BadRequestObjectResult("Invalid Parameter: The incoming tenantBranding parameter is null");
}
if (tenantBranding.TenantId == Guid.Empty)
{
return new BadRequestObjectResult("Invalid Parameter: The tenantId in the incoming parameter tenantBranding is empty");
}
var result = _brandingLogic.Save(tenantBranding).Result;
if (!result)
{
return new JsonResult(500);
}
return Ok();
}
いくつかの奇妙な理由から、TenantbrandingパラメータはApi Postメソッドではnullです。 ModelBinding fra Web ControllerとApi Controllerの間に問題があります。
これは私のTenantBrandingモデル(API)がどのように見えるかされています
public class TenantBranding
{
public Guid TenantId { get; set; }
public IEnumerable<FormFile> LogoFile { get; set; }
public IEnumerable<FormFile> BackgroundFile { get; set; }
public string DocumentType { get; set; }
}
これは、Webでの私のTenantBrandingがどのように見えるかです:
public class TenantBranding
{
public Guid TenantId { get; set; }
public IFormFile LogoFile { get; set; }
public IFormFile BackgroundFile { get; set; }
}
しかし、API方式でtenantBrandingパラメータが表示されていますnullなので、なぜこれが起こっているのか分かりません。 IFormFile
に関連していますか?