c# – WebGet在功能上等同于WebInvoke(Method =“GET”)吗?

This问题已经问到我在问什么,但我想对答案做一些澄清.

答案表明WebGet和WebInvoke类似,主要区别在于Method参数.

但是,如果Method参数设置为“GET”,它实际上是功能相同的,还是存在其他差异?

最佳答案 它们只是标记属性,最终在功能上等同于100%.解释这些属性的唯一因素是WebHttpBehavior :: GetWebMethod方法,其功能很简单:

internal static string GetWebMethod(OperationDescription od)
{
    WebGetAttribute webGetAttribute = od.Behaviors.Find<WebGetAttribute>();
    WebInvokeAttribute webInvokeAttribute = od.Behaviors.Find<WebInvokeAttribute>();
    WebHttpBehavior.EnsureOk(webGetAttribute, webInvokeAttribute, od);
    if (webGetAttribute != null)
    {
        return "GET";
    }
    if (webInvokeAttribute == null)
    {
        return "POST";
    }
    return webInvokeAttribute.Method ?? "POST";
}
点赞