MTplus
MTplus

Reputation: 2391

web.api cannot test action

I have created a Web.API, MVC application but ran into some trouble when trying to test it. I get this error:

"{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:60104/api/order/SayWhat/33'.","MessageDetail":"No type was found that matches the controller named 'order'."}"

OrderController

[System.Web.Http.HttpGet]
public string SayWhat(string quoteName)
{
    return "Hello World " + quoteName;
}

WebApiConfig

config.Routes.MapHttpRoute(
    name: "DefaultPostApi",
    routeTemplate: "api/{controller}/{action}/{quote}"
);

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

Upvotes: 0

Views: 73

Answers (3)

Manjunatha DL
Manjunatha DL

Reputation: 346

Use below code in WebApiConfig.cs

public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

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

And In Controller :

[HttpGet]
    [ActionName("SayWhat")]
    public string SayWhat(string quoteName)
    {
        return "Hello World " + quoteName;
    }

Upvotes: 3

Nkosi
Nkosi

Reputation: 247018

The error message indicates that it is not seeing the Order controller.

"No type was found that matches the controller named 'order'."

Given that you indicated that you indeed have an OrderController then one possibility is that the controller is not of the correct type as the framework looks for types that inherit from ApiController.

Make sure the controller is of the correct type

public class OrderController : ApiController {    
    //GET api/Order/saywhat/33
    [System.Web.Http.HttpGet]
    public string SayWhat(string quote) {
        return "Hello World " + quote;
    }
}

Upvotes: 0

BlackThunder
BlackThunder

Reputation: 15

try to add the attribute "ActionName" to the function.

[System.Web.Http.HttpGet()]
[ActionName("SayWhat")]
public string SayWhat(string quoteName)
{
    return Convert.ToString("Hello World ") + quoteName;
}

Upvotes: 0

Related Questions