Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No controller is found by Web API although controller exists

I created one solution with two projects: one is a class library with a Self Host Web API (created with the help of http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api), the second is a windows service created with TopShelf. The purpose of this solution is to have a status report on the service with the use of Web API.

Everything works fine, but when I recreate my solution within a target solution the whole application does not work properly. The Windows Service seems to be working, but when I type localhost:8080/Test which is suppose to view OK (and it does in the separate test solution mentioned at the beginning) it throws an error (viewed as an xml):

Message: No HTTP resource was found that matches the request URI 'http://localhost:8080/Test'.
MessageDetail: No type was found that matches the controller named 'Report'.

There is a ReportController (inheriting from ApiController) in the project that contains the SelfHost but somehow it's "visible". I took a guess (a stupid guess, I believe) and moved it to the windows service project but it's also not working.

Can someone tell me what is the problem I'm facing? Why does it not see the controller if it has in a simple solution?

EDIT: My routing looks like this:

var config = new HttpSelfHostConfiguration(String.Format("http://localhost:{0}", port));
config.Routes.MapHttpRoute("API Default", "{action}", new { controller = defaultControllerName });

where

defaultControllerName = "Report";
like image 840
Maurice Klimek Avatar asked Apr 10 '13 14:04

Maurice Klimek


2 Answers

I am ashamed to admit it, but the reason why it didn't work lay in the controller class not having an access modifier. Making it public fixed the bug.

like image 55
Maurice Klimek Avatar answered Nov 15 '22 10:11

Maurice Klimek


the Class and method must be public

public class PrintController: ApiController
{
    //[HttpGet, Route("api/Print/Getp")]
    public string Get()
    {
 
        var ob = new List<string>();
        foreach (var item in File.ReadLines(@"c:\PrintService\pr.txt"))
        {
            string i = item;
            ob.Add(i);
        }
        var json1 = JsonConvert.SerializeObject(ob);


        return "ok";
    }
}

and my route config is this:

            _config.Routes.MapHttpRoute("DefaultHttpRoute", "api/{controller}");
like image 32
Jalal Malekpour Avatar answered Nov 15 '22 12:11

Jalal Malekpour