简体   繁体   English

控制器中自定义方法的WebApi路由

[英]WebApi Routing for Custom Method in Controller

I've recently picked up the ASP.NET Web API stuff and whilst I've cracked authorisation and authentication, I can't crack routing. 我最近选择了ASP.NET Web API的东西,虽然我已经破解了授权和身份验证,但我无法破解路由。 It's a nightmare! 这是一场噩梦!

I have created a Authenticate() method with an AuthenticationController . 我已经创建了一个Authenticate()与方法AuthenticationController I add the [HttpGet] attribute to Authenticate() and yet whenever I hit the API I get a 404 . 我将[HttpGet]属性添加到Authenticate() ,但每当我点击API时,我得到404

Here is my current WebApiConfig :- 这是我目前的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 am trying is: /authentication . 我正在尝试的地址是: /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 anyone direct me in the right direction please? 任何人都可以指导我朝正确的方向发展吗? I've tried hitting multiple addresses/endpoints:- 我试过点击多个地址/端点: -

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

as per my comment: 根据我的评论:

why do you have 4 routes? 为什么你有4条路线? 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 如果删除或注释掉最后3条路线并使用web api,则执行GET时应该触发Authenticate方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM