c# - 如何在Azure Functions中以简单的方式获取GET查询参数?

标签 c# azure azure-functions

我尝试了以下方法:

/// <summary>
/// Request the Facebook Token
/// </summary>
[FunctionName("SolicitaFacebookToken")]
[Route("SolicitaToken/?fbAppID={fbAppID}&fbCode={fbCode}&fbAppSecret={fbAppSecret}")]
public static async Task<HttpResponseMessage> SolicitaFacebookToken(
    [HttpTrigger(AuthorizationLevel.Function, methods: new string[] { "get" } )]
    HttpRequestMessage req,
    TraceWriter log,
    string fbAppID,
    string fbCode,
    string fbAppSecret
)
{ }

当我访问 URL 时:

http://localhost:7071/api/SolicitaFacebookToken/?fbAppID=ABC&fbCode=DEF&fbAppSecret=GHI

但它给出了这些错误:

'SolicitaFacebookToken' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?
System.InvalidOperationException : 'SolicitaFacebookToken' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK
attributes?
 at Microsoft.Azure.WebJobs.JobHost.Validate(IFunctionDefinition function,Object key)
 at async Microsoft.Azure.WebJobs.JobHost.CallAsync(??)
 at async Microsoft.Azure.WebJobs.Script.ScriptHost.CallAsync(String method,Dictionary\`2 arguments,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostManager.HandleRequestAsync(FunctionDescriptor function,HttpRequestMessage request,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.Host.FunctionRequestInvoker.ProcessRequestAsync(HttpRequestMessage request,CancellationToken cancellationToken,WebScriptHostManager scriptHostManager,WebHookReceiverManager webHookReceiverManager)
 at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.<>c__DisplayClass3_0.<ExecuteAsync>b__0(??)
 at async Microsoft.Azure.WebJobs.Extensions.Http.HttpRequestManager.ProcessRequestAsync(HttpRequestMessage request,Func`3 processRequestHandler,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ExecuteAsync(HttpControllerContext controllerContext,CancellationToken cancellationToken)
 at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
 at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.WebScriptHostHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.SystemTraceHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
 at async System.Web.Http.HttpServer.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)

如果我改成这样:


HttpRequestMessage req,
string fbAppID,
string fbCode,
string fbAppSecret,
TraceWriter log

The following 1 functions are in error:

SolicitaFacebookToken: Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1.SolicitaFacebookToken'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'fbAppID' to type String. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

在Azure Functions模板代码中,有

string name = req.GetQueryNameValuePairs()
                 .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                 .Value;

我想要一种更简单的方法来获取 GET 查询参数。

我想要一个像这样的网址:

http://localhost:7071/api/SolicitaFacebookToken/?fbAppID=123&fbCode=456&fbAppSecret=789

:如何轻松获取参数及其值?

最佳答案

对于 v2/beta/.NET Core 运行时,您可以具体执行以下操作:

string fbAppID = req.Query["fbAppID"];

或更多通用:

using System.Collections.Generic;
...
IDictionary<string, string> queryParams = req.GetQueryParameterDictionary();
// Use queryParams["fbAppID"] to read keys from the dictionary.

对于 v1 函数应用(.NET 完整框架):

using System.Collections.Generic;
...
IDictionary<string, string> queryParams = req.GetQueryNameValuePairs()
    .ToDictionary(x => x.Key, x => x.Value);
// Use queryParams["fbAppID"] to read keys from the dictionary.

关于c# - 如何在Azure Functions中以简单的方式获取GET查询参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49833056/

相关文章:

c# - Microsoft Office 应用程序的主要互操作程序集

c# - Blazor WebAssembly 访问数据库

c# - 将书签链接与 url 路由一起使用时出现问题

azure - Application Insights 是否跟踪引荐来源网址?

node.js - Azure函数JS : get image from blob and transform to base64

c# - 仅由我的网站调用 Web Api

Azure Kubernetes 私钥位置

.net - Web 角色和云服务的多站点策略

python - 通过 url 将参数传递给 python azure 函数

azure - 如何从 Azure python sdk 创建 Azure Functions?