Search code examples
c#asp.nethttpasp.net-web-api

Can't get response from ASP.NET web API


I'm setting up an ASP.net web API, but I keep getting 404 from all Http routes.

This is running on http://localhost:52011/ since that is the default location that VS2013 uses. I've added a simple test HttpRoute that just returns a string if it's called with a GET to test connectivity.

Directory browsing is enabled for feedback that the web API is in fact on localhost, and all directories of the project are visible.

I've tried setting the controller name fixed in the routes in WebApiConfig.cs and read a lot about the HttpRoute mappings, but I keep getting either a 404 or a:

"No Type was found that matches the controller named controllername".

I'm using Postman to test since that was recommended and easy to use.

Any tips are welcome!

WebApiConfig.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace BadgePrinter
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {            
            config.MapHttpAttributeRoutes();
            //route to print badge.
            config.Routes.MapHttpRoute(
                name: "apiPrint",
                routeTemplate: "api/{controller}/{action}/{Bedrijf}/{Aanspreektitel}/{Voornaam}/{Achternaam}/{Jobtitle}/{Kopies}/{Rijksregisternummer}/{BadgeAfdruk}/{printer}/{Image}",
                defaults: new { Image = RouteParameter.Optional}
            );
            //test route to get string back
            config.Routes.MapHttpRoute(
                name: "apiTest",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

BadgeController.cs (the only controller in the project)

The first method is the one I'm going to have to use eventually, but the testmethod is the one I use for testing.

using BadgePrinter.CardTemplates;
using BadgePrinter.Interfaces;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace BadgePrinter.Controllers
{
    public class BadgeController : ApiController
    {     
        //removed the first method since it was big and distracting.
        // test method
        [HttpGet]
        public IHttpActionResult GetAllProducts()
        {
            return Ok("some product");
        }
    }
}

Link for the testmethod that I'm using: http://localhost:52011/api/BadgeController/Products/1

I also tried this link, even though that shouldn't matter: http://localhost:52011/api/BadgeController/Products/1

Result:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:52011/api/BadgeController/Products/1'.",
    "MessageDetail": "No type was found that matches the controller named 'BadgeController.cs'."
}

For the other HttpRoute, I keep getting 404, but I'll be in heaven if I can get the test to work :)

PS: I'm new to this kind of thing (ASP.net and API's) so I'm sorry if I'm being an idiot.


Solution

  • You have 2 issues here:

    1) Your controller name BadgeController but in your API it should be Badge only. The correct URL should be:

    http://localhost:52011/api/Badge/xxx
    

    2) I can't see method name Products or something like that in your code.

    3) If it is a POST method, it must have [FromBody] in parameter input.