Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 获取404';使用MVC5API控制器的s_Asp.net_Asp.net Mvc_Asp.net Web Api_Asp.net Mvc 5 - Fatal编程技术网

Asp.net 获取404';使用MVC5API控制器的s

Asp.net 获取404';使用MVC5API控制器的s,asp.net,asp.net-mvc,asp.net-web-api,asp.net-mvc-5,Asp.net,Asp.net Mvc,Asp.net Web Api,Asp.net Mvc 5,控制器: public class DownloadListController : ApiController { private MovieService _movieService; public DownloadListController() { _movieService = new MovieService(); } public void Post([FromBody]string asd) {

控制器:

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();
}
公共静态无效寄存器(HttpConfiguration配置)
{
config.maphttpAttribute路由();
config.Routes.MapHttpRoute(
名称:“DefaultApi”,
routeTemplate:“api/{controller}/{action}/{id}”,
默认值:新建{id=RouteParameter.Optional}
);
//取消注释以下代码行,以启用对具有IQueryable或IQueryable返回类型的操作的查询支持。
//要避免处理意外或恶意查询,请使用QueryableAttribute上的验证设置验证传入的查询。
//有关更多信息,请访问http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
//要在应用程序中禁用跟踪,请注释掉或删除以下代码行
//有关更多信息,请参阅:http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
当我运行应用程序并浏览到
http://localhost:4229/api/DownloadList
我得到一个404,而我可以在网络中看到发出的请求是一个
get
请求

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


我错过了什么?我需要做什么来修复此问题?

Web API不使用显式操作。它们由HTTP动词暗示。因此,您的
路由模板中不应该有
{action}

routeTemplate: "api/{controller}/{id}",

注意:WebAPI不是MVC。它们不是“MVC5API控制器”,而是WebAPI 2.x API控制器碰巧WebAPI是在MVC4中引入的,但它是一组与MVC一起使用的不同技术。