使用 MVC 5 Api 控制器获得 404

Get 404's with MVC 5 Api controllers

控制器:

public class DownloadListController : ApiController
{
    private MovieService _movieService;

    public DownloadListController()
    {
        _movieService = new MovieService();
    }

    public void Post([FromBody]string asd)
    {
        // Do something
    }

    public string Get()
    {
        return "test";
    }
}

Global.asax

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    // WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    GlobalConfiguration.Configure(WebApiConfig.Register);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

WebApiConfig

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
    // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
    // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
    //config.EnableQuerySupport();

    // To disable tracing in your application, please comment out or remove the following line of code
    // For more information, refer to: http://www.asp.net/web-api
    config.EnableSystemDiagnosticsTracing();
}

当我 运行 应用程序并浏览到 http://localhost:4229/api/DownloadList 时,我得到一个 404,而我在网络中看到发出的请求是 GET 请求。

当我向 URI 发出 POST 请求时也是如此,结果是 404。

我错过了什么?我需要做什么来解决这个问题?

Web API 不使用显式操作。它们由 HTTP 动词暗示。所以你的 routeTemplate:

中不应该有 {action}
routeTemplate: "api/{controller}/{id}",