任务异步控制器方法未命中 [英] Task async controller method does not hit

查看:75
本文介绍了任务异步控制器方法未命中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我们有一个MVC项目,该项目已通过MVC的不同版本从1升级到4。

So, we've got an MVC project that has been upgraded through the different versions of MVC from 1 through to 4.

现在我们有一个控制器方法:

Now we have a controller method:

public async Task<ActionResult> IndexAsync()

所以如果我们去 http://somedomain.xyz / WhicheverController http://somedomain.xyz/WhicheverController/Index ,会收到一个404。

so if we go to http://somedomain.xyz/WhicheverController or http://somedomain.xyz/WhicheverController/Index, we are greeted with a 404.

http://somedomain.xyz/WhicheverController/IndexAsync 路由到该方法很好。

我们的路由出了什么问题?

What's gone wrong with our routing?

推荐答案

I相信如果您从AsyncController派生Controller,那么您的示例将起作用。

I believe that your example will work if you derive your Controller from AsyncController instead.

public class MyController:AsyncController
{
    public async Task<ActionResult> IndexAsync()
    {
       return View(); //view called "Index.cshtml", not "IndexAsync.cshtml"
    }
}

所以现在您可以打〜/ My / Index 而没有 Async 后缀,尽管<$ c控制器名称中出现$ c> Async 。

So now you can hit ~/My/Index without the Async suffix, despite Async appearing in the controller name.

这是以前的MVC异步控制器方法的结果,通常需要 IndexComplete 方法可以工作,但是使用基于任务的异步控制器方法时,不需要匹配的 XxxxComplete 方法,但是<$ c遵守$ c> Async 约定。

This is a relic from the previous MVC asynchronous controller method, and usually required an IndexComplete method to work, but with Task based async controller method, the matching XxxxComplete method is not required, but the Async convention is observed.

AsyncController 的实际实现是稀疏:

public abstract class AsyncController : Controller
{
}

因此在MVC堆栈中的某处,将测试控制器的类型,并打开特殊的路由魔术。

So somewhere in the MVC stack, the type of the controller is tested, and special routing magic is turned on.

这篇关于任务异步控制器方法未命中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆