首页 文章

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

提问于
浏览
1

我试过了

/// <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

但它给出了这些错误:

无法从Azure WebJobs SDK调用“SolicitaFacebookToken” . 是否缺少Azure WebJobs SDK属性? System.InvalidOperationException:无法从Azure WebJobs SDK调用“SolicitaFacebookToken” . 是否缺少Azure WebJobs SDK属性?在Microsoft.Azure.WebJobs.JobHost.Validate(IFunctionDefinition函数,对象键)atync Microsoft.Azure.WebJobs.JobHost.CallAsync(??)async Microsoft.Azure.WebJobs.Script.ScriptHost.CallAsync(String method,Dictionary)在异步Microsoft.Azure.WebJobs.Script.Host.FunctionRequestInvoker.ProcessRequestAsync(HttpRequestMessage请求)中的异步Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostManager.HandleRequestAsync(FunctionDescriptor函数,HttpRequestMessage请求,CancellationToken cancellationToken)中的2参数,CancellationToken cancellationToken) ,CancellationToken cancellationToken,WebScriptHostManager scriptHostManager,WebHookReceiverManager webHookReceiverManager)async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController . <> c__DisplayClass3_0.b__0(??)async Microsoft.Azure.WebJobs.Extensions.Http.HttpRequestManager.ProcessRequestAsync (异步微软的HttpRequestMessage请求,Func3 processRequestHandler,CancellationToken cancellationToken) t.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ExecuteAsync(HttpControllerContext controllerContext,CancellationToken cancellationToken)atync System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)async System.Web.Http . 在异步Microsoft.Azure.WebJobs.Script.WebHost.Handlers上的异步Microsoft.Azure.WebJobs.Script.WebHost.Handlers.WebScriptHostHandler.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)上的Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken) . 异步System.Web.Http.HttpServer.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)中的SystemTraceHandler.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)

如果我换到

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

[14/04/2018 15:24:49]以下1个函数出错:[14/04/2018 15:24:49] SolicitaFacebookToken:Microsoft.Azure.WebJobs.Host:错误索引方法'Function1.SolicitaFacebookToken ” . Microsoft.Azure.WebJobs.Host:无法将参数'fbAppID'绑定到String类型 . 确保绑定支持参数Type . 如果您正在使用绑定扩展(例如ServiceBus,Timers等),请确保您已在启动代码中调用扩展的注册方法(例如config.UseServiceBus(),config.UseTimers()等) .

在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

并轻松获取参数及其值 .

How to do it?

2 回答

  • 1

    在过去,当我有多个参数时,我已将它们添加到路线中 . 所以不是这样的:

    [Route("SolicitaToken/?fbAppID={fbAppID}&fbCode={fbCode}&fbAppSecret={fbAppSecret}")]
    

    我做过这样的事情:

    [Route("SolicitaToken/{fbAppID}/{fbCode}/{fbAppSecret}")]
    

    然后您根本不需要访问查询字符串,可以直接使用函数参数 .

    [FunctionName("Function1")]
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "HttpTriggerCSharp/name/{fbAppID}/{fbCode}/{fbAppSecret}")]HttpRequestMessage req, string fbAppID, string fbCode, string fbAppSecret, TraceWriter log)
    {
      log.Info("C# HTTP trigger function processed a request.");
      var msg = $"App ID: {fbAppID}, Code: {fbCode}, Secret: {fbAppSecret}";
      // Fetching the name from the path parameter in the request URL
      return req.CreateResponse(HttpStatusCode.OK, msg);
    }
    
  • 1

    对于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 Full Framework):

    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.
    

相关问题