Get 404's with MVC 5 Api controllers

Controller:

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

When I run the application and browse to http://localhost:4229/api/DownloadList I get an 404, while I can see in the Network request being made is a GET request.

The same is also true when I make a POST request to a URI, the result is a 404.

What am I missing? What do I need to do to fix this?

Answers


The Web API doesn't use explicit actions. They are implied by the HTTP verbs. So you shouldn't have an {action} in your routeTemplate:

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

Need Your Help

missing partial when putting a model in another model

ruby-on-rails ruby-on-rails-3 activeadmin nested-forms

I have never seen an error like this, so I don't even know what information of my app to share with you. I am putting an 'address' model inside a 'customer model'. Here is the error. (edit)I am using

Hide input-help on form using AngularJS

javascript jquery angularjs

I'm currently learning AngularJS and i would like to accomplish a simple task.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.