I am currently learning how to create C# server with OWIN and Katana.
I am trying to respond to a POST, but unfortunately it does not find the function.
So this is what I have:
This is a user side class, which is sending user data (username and password) via POST (PostAsJsonAsync()).
public class UserRegisterClient
{
string _accessToken;
Uri _baseRequestUri; // http://localhost:8080
public UserRegisterClient(Uri baseUri, string accessToken)
{
_accessToken = accessToken;
_baseRequestUri = new Uri(baseUri, "api/register/");
}
// Handy helper method to set the access token for each request:
void SetClientAuthentication(HttpClient client)
{
client.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("Bearer", _accessToken);
}
public async Task<HttpStatusCode> AddUserAsync(string username, string password)
{
HttpResponseMessage response;
using (var client = new HttpClient())
{
SetClientAuthentication(client);
response = await client.PostAsJsonAsync(
_baseRequestUri.ToString(), new KeyValuePair<string, string>(username, password));
}
return response.StatusCode;
}
}
Additional information:
in the AddUserAsync function client.PostAsJsonAsync() returns the following:
response =
{
StatusCode: 404,
ReasonPhrase: 'Not Found',
Version: 1.1,
Content: System.Net.Http.StreamContent,
Headers:
{
Date: Sat, 11 Jul 2015
18:16:53 GMT Server: Microsoft-HTTPAPI/2.0 Content-Length: 190
Content-Type: application/json; charset=utf-8
}
} System.Net.Http.HttpResponseMessage
On the server side, I have a controller, that looks like this:
[RoutePrefix("api/register/")]
class RegisterController : ApiController
{
public async Task<IHttpActionResult> Post(KeyValuePair<string, string> userData)
{
// I never get inside here
}
}
On the server side, in the Startup class, you can see the route setup:
private HttpConfiguration ConfigureWebApi()
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
return config;
}
Edit: changed Route to RoutePrefix before my controller class.
Make the Controller class public.
[Route("api/register/")]
public class RegisterController : ApiController
Not being public means that the Web API system cannot discover the controller and its actions.