4

我最近拿起了 ASP.NET Web API 的东西,虽然我已经破解了授权和身份验证,但我无法破解路由。这是一场噩梦!

我创建了一个Authenticate()带有AuthenticationController. 我将[HttpGet]属性添加到Authenticate(),但每当我点击 API 时,我都会得到一个404.

这是我目前的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) });
}

我正在尝试的地址是:/authentication.

控制器看起来像这样:-

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

      }
}

谁能指导我正确的方向?我试过打多个地址/端点:-

  • /authentication/authenticate
  • /authentication/
  • /authentication
4

1 回答 1

1

as per my comment:

why do you have 4 routes? I'm not sure what you're trying to achieve exactly exactly but you could most likely achieve it with the top (default) route only. if you remove or comment out the last 3 routes and youre using web api then your Authenticate method should be hit when you perform a GET

于 2012-09-23T14:22:07.877 回答