是否有应用于 Web API ApiController 方法的默认动词? [英] Is there a default verb applied to a Web API ApiController method?

查看:30
本文介绍了是否有应用于 Web API ApiController 方法的默认动词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看这篇文章中的代码(在 https://github.com/patelsan/WebAPIAuthentication 中): http://www.codeproject.com/Articles/630986/Cross-Platform-Authentication-With-ASP-NET-Web-API.

I've been looking at the code (in https://github.com/patelsan/WebAPIAuthentication) from this article: http://www.codeproject.com/Articles/630986/Cross-Platform-Authentication-With-ASP-NET-Web-API.

它非常好,似乎工作正常.解释这种令牌认证的文章很少,但这是我见过的最好的.请注意,我是这项技术的新手,还有很多东西要学.

It's pretty good and seems to work fine. There are very few articles that explain this kind of token authentication, but this is the best I've seen. Note that I'm new to this technology and there's much to learn.

所以,我注意到 UsersController 有这个代码:

So, I noticed that the UsersController has this code:

public class UsersController : ApiController
{
    public Status Authenticate(User user)
    {
        . . .
    }
}

Authenticate 方法不是以已知的 HTTP 动词开头,例如Get 或 Post,并且没有与此方法关联的 [HttpGet] 或 [HttpPost] 属性,那么控制器如何知道该方法与哪个动词关联呢?只看代码,我怎么知道我需要使用哪个动词?如果没有匹配项,是否有默认"动词之类的东西?

The Authenticate method doesn't start with a known HTTP verb, e.g. Get or Post, and there's no [HttpGet] or [HttpPost] attribute associated with this method, so how does the controller know with which verb to associate this method? Just by looking at the code, how can I tell which verb I need to use? Is there such a thing as a "default" verb if nothing matches?

顺便说一下,如果您想知道,唯一有效的动词是 POST.我很想知道为什么会这样.

By the way, in case you're wondering, the only verb that works is POST. I'd love to understand why that is the case.

推荐答案

在每天学习新事物下提交此文件!

File this under learning something new every day!

通常方法名称匹配是 这样想.但是,查看 WebAPI 源代码,有一个用于回退的逻辑分支.如果方法名称未(通过属性或约定)映射到支持的 HTTP 动词,则默认为 POST.

Typically method name matching is thought of this way. Looking at the WebAPI source, however, there is a branch of logic for fallback. If the method name doesn't map (through attribute, or convention) to a supported HTTP verb, then the default is POST.

默认情况下,动作选择通过 ReflectedHttpActionDescriptor 类发生.这里的重要方法是GetSupportedHttpMethods().在相关部分,代码如下:

By default action selection happens through ReflectedHttpActionDescriptor class. The important method here is GetSupportedHttpMethods(). In relevant part the code reads:

        if (supportedHttpMethods.Count == 0)
        {
            // Use POST as the default HttpMethod
            supportedHttpMethods.Add(HttpMethod.Post);
        }

您可以在此处查看完整源代码(在文件中间).

You can see the full source here (around the middle of the file).

这篇关于是否有应用于 Web API ApiController 方法的默认动词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆