Reputation: 90
I'm trying to call the action FileUploadAsync
from the view. The refresh button that just calls the Index action is working perfectly. The upload button on the view is returning 404 not found... can't think of a reason why.
Controller:
[Area("Admin")]
public class MetricController : Controller
{
public async Task<IActionResult> Index()
{
var allBlobs = await _azureBlobService.ListAsync();
return View(allBlobs);
}
public async Task<IActionResult> FileUploadAsync()
{
var request = await HttpContext.Request.ReadFormAsync();
if (request.Files == null)
{
return BadRequest("files not uploaded");
}
var files = request.Files;
if (files.Count == 0)
{
return BadRequest("files empty");
}
await _azureBlobService.UploadAsync(files);
return RedirectToAction("Index");
}
}
View:
<div class="container-fluid">
<div class="btn btn-primary btn-sm">
<span>Select Files</span><input type="file" id="file" name="selectFiles" class="upload" onchange="DisplayFilesToUpload()" multiple />
</div>
<p id="FilesToUpload"></p>
@if (Model != null && Model.Count > 0)
{
foreach (var item in Model)
{
<div>
<p class="text-secondary">@item</p>
</div>
}
}
<a asp-area="Admin" asp-controller="Metric" asp-action="Index" class="btn btn-outline-primary btn-sm">
Refresh
</a>
<a asp-area="Admin" asp-controller="Metric" asp-action="FileUploadAsync" class="btn btn-outline-primary btn-sm">
Upload
</a>
<a asp-action="DeleteAll" class="btn btn-outline-danger btn-sm">
Delete All
</a>
@section scripts{
<script type="text/javascript" src="~/js/metrics.js"></script>
}
</div>
Edit:
On Startup.cs
the routing is deffined as follows
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{area=Agent}/{controller=Article}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
So this sholudn't be the issue...
Upvotes: 2
Views: 1497
Reputation: 20116
This has been discussed in github:https://github.com/aspnet/AspNetCore/issues/8998
Async
suffix for controller action names will be trimmed by default in asp.net core 3.0.
Prior to 3.0, the action will be routeable via Admin/Metric/FileUploadAsync
. Link generation would require specifying the Async suffix e.g.
<a asp-area="Admin" asp-controller="Metric" asp-action="FileUploadAsync" class="btn btn-outline-primary btn-sm">
Upload
</a>
In 3.0, the action will be routeable via Admin/Metric/FileUpload
and link generation would require not specifying the Async suffix.
One solution is that you could change your view code to below:
<a asp-area="Admin" asp-controller="Metric" asp-action="FileUpload" class="btn btn-outline-primary btn-sm">
Upload
</a>
The other solution is that you could just disable this behavior , add below code in startup ConfigureServices:
services.AddMvc(options =>
{
options.SuppressAsyncSuffixInActionNames = false;
});
Upvotes: 3
Reputation: 4634
Try changing your view to this:
<a asp-area="Admin" asp-controller="Metric" asp-action="FileUpload" class="btn btn-outline-primary btn-sm">
Upload
</a>
Reference: Async suffix removal from controller action names
Upvotes: 0