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

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

问题描述

我已经看$ C $℃(在<一个href=\"https://github.com/patelsan/WebAPIAuthentication\">https://github.com/patelsan/WebAPIAuthentication)从这篇文章:<一href=\"http://www.$c$cproject.com/Articles/630986/Cross-Platform-Authentication-With-ASP-NET-Web-API\">http://www.$c$cproject.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.

这是pretty好,似乎很好地工作。有迹象表明,解释这种令牌认证的极少数文章,但是这是我见过的最好的。请注意,我是新来这个技术,还有很多东西要学。

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有这个code:

So, I noticed that the UsersController has this code:

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

身份验证方法不与已知的HTTP动词,例如启动GET或POST,而且也没有[HTTPGET]或[HttpPost]与此方法关联属性,因此如何控制器知道与动词这种方法关联?只需通过查看code,我怎么能告诉我需要使用的动词?是否有这样的事,作为一个默认的动词如果没有相匹配?

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!

通常方法名称匹配是<一个href=\"http://stackoverflow.com/questions/10439738/how-does-a-method-in-mvc-webapi-map-to-an-http-verb/10471854#10471854\">thought这种方式。综观的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()。在相关部分的code读取:

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);
        }

您可以看到<一个href=\"https://aspnetwebstack.$c$cplex.com/SourceControl/latest#src/System.Web.Http/Controllers/ReflectedHttpActionDescriptor.cs\">full来源此处(围绕文件的中间)。

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

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

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