3

I have an API that works fine locally and when I move it to the live environment it doesn't.

The main POST action on the affected controller returns:

NotFound

With a test GET action I get back:

"Message": "No HTTP resource was found that matches the request URI

Strangely, when I uploaded a testController with the same test action as used in the main controller I get a proper response from the API.

This is the test that works fine:

public class TestController : ApiController
{

    [AllowAnonymous]
    [HttpGet]
    public HttpResponseMessage helloWorld()
    {
        return Request.CreateResponse(HttpStatusCode.OK, "HelloWorld!");
    }
}

The controller which does not work:

public class DeviceController : ApiController
{

    [AllowAnonymous]
    [HttpGet]
    public HttpResponseMessage helloWorld() // This returns: "No HTTP resource was found that matches the request URI 'http://api.mySite.com/api/Device/helloWorld'."
    {
        return Request.CreateResponse(HttpStatusCode.OK, "HelloWorld!");
    }

    [AllowAnonymous]
    [HttpPost]
    public HttpResponseMessage Login([FromBody] LoginObject loginObject) // This returns: "NotFound"
    {
        ...
    }

}

Here is the web config:

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

        config.Routes.MapHttpRoute(

            name: "API Default",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }

        );
    }
}
4
  • So http://api.mySite.com/api/Test/helloWorld works, right? Commented Jan 16, 2017 at 11:16
  • yeah indeed, I get "HelloWorld!" back. This seems so odd... got to be a routing issue? Commented Jan 16, 2017 at 11:36
  • Hmm... lets check it. try to add route attribute [Route("api/Device/helloWorld")] Commented Jan 16, 2017 at 11:38
  • ahh, ok it is now working. I wonder why the routing for the other controller is ok without explicitly declaring it... Feel free to add this as an answer and I will accept it. Thanks! Commented Jan 16, 2017 at 11:46

3 Answers 3

4

Try to add explicitly declare of route like by acrion

[Route("api/Device/helloWorld")]
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage helloWorld() 

or

[RoutePrefix("api/Device")]
public class DeviceController : ApiController

and then

[Route("helloWorld")]
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage helloWorld() 
Sign up to request clarification or add additional context in comments.

1 Comment

Explicitly declaring the routes solved this issue for me, thanks.
3

For poor sap's like myself in the future: Ensure the methods on your controller are public.

Comments

3

I spent some time looking for the answer to this problem in .NET 7.0 after I had made a new project (which automatically created a WeatherForecastController).

It turns out that the project had also automatically created a file named proxy.conf.js. In the file, the context: setting was set to "/weatherforecast". I changed it to "/api" instead and then changed [Route("[controller]")] to [Route("api/[controller]")] in both controller files. The controllers worked fine after that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.