AndrewG
AndrewG

Reputation: 145

No Type was found that matches Controller non-MVC ASP.net Web API

Currently I am creating a web application using a non-mvc web API in .NET. We are also using AngularJS to call the API with.

Here is the Router:

public static class WebApiConfig
{
    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 }
        );
    }
}

Here is the controller:

public class signOnController : ApiController
{
    public IHttpActionResult GetSingleSignOn()
    {
        singleSignOn sign = new singleSignOn();
        var check = sign.executeStoredProcedure();

        if (check == 0)
        {
            return Ok("http://localhost:64325/EmployeeIndex.html#/form/begin");
        }
        else if (check == 1)
        {
            return Ok("http://localhost:64325/ManagerIndex.html#/form/begin");
        }
        else
        {
            return Unauthorized();
        }

    }

}

Here is the angularJS:

formApp.service('ApiCall', ['$http', function ($http) {
    var result;

    this.GetApiCall = function (controllerName, methodName) {
     result = $http.get('api/' + controllerName + '/' + methodName).success(function (data, status) {
        result = data;
    }).error(function () {
        alert("API Get call error");
    });
    return result;
};

I keep getting this error when I try to run it:

<Error>
    <Message>
        No HTTP resource was found that matches the request URI 'http://localhost:51463/api/signOnController/GetSingleSignOn'.
    </Message>
    <MessageDetail>
        No type was found that matches the controller named 'signOnController'.
    </MessageDetail>
</Error>

Anyone know what I'm doing wrong with it?

Upvotes: 1

Views: 478

Answers (2)

Haseeb A
Haseeb A

Reputation: 6122

Firstly, the route should be http://localhost:51463/api/signOn/GetSingleSignOn. controller name is just for identifying it is a controller.in routes you must use this without the word Controller. but when creating a controller make sure you include the word

secondly, i think what you exactly want to achieve is a route like in MVC. change your router to include action.

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

the default asp webapi routes are for pure REST API implementation which is not suitable for most cases.

Upvotes: 0

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

You shouldn't be mentioning Controller inside route name. It should be

http://localhost:51463/api/signOn/GetSingleSignOn


Though you can use Route Attribute to define Routing to make route more verbose.

[Route("SingleSignOn")]
public IHttpActionResult GetSingleSignOn() {
    ....
}

Then URL will be like http://localhost:51463/api/signOn/SingleSignOn

Upvotes: 1

Related Questions