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.
The first thing that I would look at is what NuGet packages you have installed and make sure that everything is good in your Startup class. Your Startup class should look like this:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = ConfigureWebApi();
app.UseWebApi(config);
}
}
Make sure that you have these NuGet packages installed if you are hosting on IIS:
Try to add public
to your controller:
[Route("api/register/")]
public class RegisterController : ApiController
{
public async Task<IHttpActionResult> Post(KeyValuePair<string, string> userData)
{
// I never get inside here
}
}
Since you're using owin/katana and attribute routing you can get rid of the old style web api configuration:
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
return config;
and using this instead:
config.MapHttpAttributeRoutes();
before
app.UseWebApi(config);
Doing this you will have to prefix every single ruote in your web api but, I guess, it's easier to understand and I've heard it's going to become the standard with vNext.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.