查看在 Asp.Net core 3 中的控制器中未找到操作 [英] View not finding action in controller in Asp.Net core 3

查看:31
本文介绍了查看在 Asp.Net core 3 中的控制器中未找到操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从视图中调用操作 FileUploadAsync.仅调用索引操作的刷新按钮工作正常.视图上的上传按钮返回 404 not found... 想不出原因.

控制器:

[Area("Admin")]公共类 MetricController :控制器{公共异步任务指数(){var allBlobs = await _azureBlobService.ListAsync();返回视图(allBlobs);}公共异步任务文件上传异步(){var request = await HttpContext.Request.ReadFormAsync();if (request.Files == null){return BadRequest("文件未上传");}var 文件 = request.Files;if (files.Count == 0){return BadRequest("文件为空");}等待 _azureBlobService.UploadAsync(files);return RedirectToAction("索引");}}

查看:

<div class="btn btn-primary btn-sm"><span>选择文件</span><input type="file" id="file" name="selectFiles" class="upload" onchange="DisplayFilesToUpload()" 多个/>

<p id="FilesToUpload"></p>@if (模型!= null && Model.Count > 0){foreach(模型中的变量项){<div><p class="text-secondary">@item</p>

}}<a asp-area="Admin" asp-controller="Metric" asp-action="Index" class="btn btn-outline-primary btn-sm">刷新</a><a asp-area="Admin" asp-controller="Metric" asp-action="FileUploadAsync" class="btn btn-outline-primary btn-sm">上传</a><a asp-action="DeleteAll" class="btn btn-outline-danger btn-sm">删除所有</a>@section 脚本{<script type="text/javascript" src="~/js/metrics.js"></script>}

Startup.cs 上,路由定义如下

app.UseEndpoints(endpoints =>{endpoints.MapControllerRoute(名称:默认",模式:{area=Agent}/{controller=Article}/{action=Index}/{id?}");端点.MapRazorPages();});

所以这应该不是问题...

解决方案

这个已经在 github 中讨论过:https://github.com/aspnet/AspNetCore/issues/8998

Async 在 asp.net core 3.0 中默认会修剪控制器操作名称的后缀.

在 3.0 之前,操作可通过 Admin/Metric/FileUploadAsync 路由.链接生成需要指定 Async 后缀,例如

上传</a>

在 3.0 中,操作将通过 Admin/Metric/FileUpload 路由,链接生成不需要指定 Async 后缀.

一种解决方案是您可以将视图代码更改为以下内容:

 上传</a>

另一种解决方案是您可以禁用此行为,在启动 ConfigureServices 中添加以下代码:

services.AddMvc(options =>{options.SuppressAsyncSuffixInActionNames = false;});

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

解决方案

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

这篇关于查看在 Asp.Net core 3 中的控制器中未找到操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆