Hello,
I am playing a bit with Web API for the first time and I am having some routing issue. The controller is basic:
public class RegisterApplicationController : ApiController
{
[HttpPost]
public HttpResponseMessage Post(JObject request)
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
return response;
}
}
and also my routing is just defining the controller:
static void Main(string[] args)
{
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration("http://localhost:4040");
config.Routes.MapHttpRoute("default", "{controller}");
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync();
Console.ReadLine();
server.CloseAsync();
}
when I make the call with Fiddler, I get an 404 error:
POST /registerapplication HTTP/1.1
User-Agent: Fiddler
Host: localhost:4040
Content-Length: 21
accept: application/json
{ "name": "value" }
HTTP/1.1 404 Not Found
Content-Length: 207
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 27 Feb 2013 06:18:53 GMT
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:4040/registerapplication'.","MessageDetail":"No type was found that matches the controller named 'registerapplication'."}
Looks like there is confusion between MVC and Web API. Can you change your route like this "api/{Controller}" and add /api/registerapplication when making a call.
MsdnDev
Member
2 Points
11 Posts
MapHttpRoute issue
Feb 27, 2013 05:25 AM|LINK
Hello,
I am playing a bit with Web API for the first time and I am having some routing issue. The controller is basic:
public class RegisterApplicationController : ApiController { [HttpPost] public HttpResponseMessage Post(JObject request) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); return response; } }and also my routing is just defining the controller:
static void Main(string[] args) { HttpSelfHostConfiguration config = new HttpSelfHostConfiguration("http://localhost:4040"); config.Routes.MapHttpRoute("default", "{controller}"); HttpSelfHostServer server = new HttpSelfHostServer(config); server.OpenAsync(); Console.ReadLine(); server.CloseAsync(); }when I make the call with Fiddler, I get an 404 error:
POST /registerapplication HTTP/1.1 User-Agent: Fiddler Host: localhost:4040 Content-Length: 21 accept: application/json { "name": "value" } HTTP/1.1 404 Not Found Content-Length: 207 Content-Type: application/json; charset=utf-8 Server: Microsoft-HTTPAPI/2.0 Date: Wed, 27 Feb 2013 06:18:53 GMT {"Message":"No HTTP resource was found that matches the request URI 'http://localhost:4040/registerapplication'.","MessageDetail":"No type was found that matches the controller named 'registerapplication'."}What do I miss?
Thanks,
Pedro
apeter
Member
4 Points
4 Posts
Re: MapHttpRoute issue
Feb 27, 2013 09:34 AM|LINK
Instead of this "server.OpenAsync();", try with "server.OpenAsync().Wait(); "
MsdnDev
Member
2 Points
11 Posts
Re: MapHttpRoute issue
Feb 27, 2013 02:03 PM|LINK
That didn't resolved the problem. It looks like a routing configuration issue.
MsdnDev
Member
2 Points
11 Posts
Re: MapHttpRoute issue
Feb 27, 2013 02:39 PM|LINK
I enabled the logs and this is what is logged:
Thanks
danroth27
Member
174 Points
40 Posts
Microsoft
Re: MapHttpRoute issue
Feb 27, 2013 02:40 PM|LINK
Senior Program Manager
ASP.NET
apeter
Member
4 Points
4 Posts
Re: MapHttpRoute issue
Feb 27, 2013 02:49 PM|LINK
Looks like there is confusion between MVC and Web API. Can you change your route like this "api/{Controller}" and add /api/registerapplication when making a call.
MsdnDev
Member
2 Points
11 Posts
Re: MapHttpRoute issue
Feb 27, 2013 11:00 PM|LINK
The controller is defined in another assembly and I discovered it wasn't loaded in memory. I created an instance of the controller in the Main:
and then it started working. I'll change my code to just load the assembly by reflection so that I can have a sort of plugin model.
Is that the correct approach?
danroth27
Member
174 Points
40 Posts
Microsoft
Re: MapHttpRoute issue
Feb 28, 2013 06:19 AM|LINK
Senior Program Manager
ASP.NET
MsdnDev
Member
2 Points
11 Posts
Re: MapHttpRoute issue
Feb 28, 2013 04:21 PM|LINK
Are you referring to IAssembliesResolver? That API requires knowing all assemblyes upfront rather than loading them when missing (lazy loading).