WebApi routing for user method in controller

I recently took ASP.NET API stuff, and although I have allowed authorization and authentication, I cannot crack routing. A nightmare!

I created an Authenticate() method with an AuthenticationController . I add the [HttpGet] attribute to Authenticate() , and yet whenever I get into the API, I get 404 .

Here is my current WebApiConfig : -

 public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute("DefaultApiWithId", "{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" }); config.Routes.MapHttpRoute("DefaultApiWithAction", "{controller}/{action}"); config.Routes.MapHttpRoute("DefaultApiGet", "{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }); config.Routes.MapHttpRoute("DefaultApiPost", "{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }); } 

And the address I'm trying to do is /authentication .

The controller looks like this: -

 [BasicHttpAuthorize(RequireAuthentication = true)] public class AuthenticationController : ApiController { [HttpGet] public string Authenticate(string email, string password, string agent, string ip) { } } 

Can someone direct me in the right direction please? I tried using multiple addresses / endpoints: -

  • /authentication/authenticate
  • /authentication/
  • /authentication
+4
source share
1 answer

according to my comment:

Why do you have 4 routes? I'm not sure what you are trying to achieve exactly, but you are most likely to achieve this only by using the top (default) route. if you delete or comment out the last 3 routes and you use web api then your authentication method should be deleted when you execute GET

+1
source

Source: https://habr.com/ru/post/1435743/


All Articles