Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

c# - View not finding action in controller in Asp.Net core 3

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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; 
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...