I'm trying to create an Azure Function that takes a wildcard route segment. However, when I try to include the wildcard segment as a method parameter I get the following error:
<ApiErrorModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Microsoft.Azure.WebJobs.Script.WebHost.Models">
<Arguments xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true"/>
<ErrorCode>0</ErrorCode>
<ErrorDetails>
Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: Functions.Page --->
System.InvalidOperationException : Exception binding parameter 'path' --->
System.InvalidOperationException : No value was provided for parameter 'path'.
at Microsoft.Azure.WebJobs.Host.Bindings.Invoke.ClassInvokeBinding`1.BindAsync(BindingContext context)
at async Microsoft.Azure.WebJobs.Host.Triggers.TriggeredFunctionBinding`1.BindCoreAsync[TTriggerValue](ValueBindingContext context,Object value,IDictionary`2 parameters)
End of inner exception at Microsoft.Azure.WebJobs.Host.Executors.DelayedException.Throw()
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,IReadOnlyDictionary`2 parameters,TraceWriter traceWriter,ILogger logger,CancellationTokenSource functionCancellationTokenSource)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)
End of inner exception at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance functionInstance,CancellationToken cancellationToken)
at Microsoft.Azure.WebJobs.Host.Executors.ExceptionDispatchInfoDelayedException.Throw()
at async Microsoft.Azure.WebJobs.JobHost.CallAsyncCore(MethodInfo method,IDictionary`2 arguments,CancellationToken cancellationToken)
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.WebHost.Controllers.FunctionsController.ProcessRequestAsync(HttpRequestMessage request,FunctionDescriptor function,CancellationToken cancellationToken)
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.SystemTraceHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.WebScriptHostHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
at async System.Web.Http.HttpServer.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
</ErrorDetails>
<Id>1efe3ac0-3f71-48a0-906e-e9410a214f5a</Id>
<Message>
Exception while executing function: Functions.Page -> Exception binding parameter 'path' -> No value was provided for parameter 'path'.
</Message>
<RequestId>9f6307a0-f9bd-4c41-9531-f6cef0ff6062</RequestId>
<StatusCode>InternalServerError</StatusCode>
</ApiErrorModel>
This is my function:
[FunctionName("Page")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "{owner}/{repo}/page/{*path}")]
HttpRequestMessage req, string owner, string repo, string path, TraceWriter log)
{
// my code
}
I have found that if I omit the path
parameter on the function the method is invoked, but then I don't have direct access to the wildcard segments.
Any ideas what might be wrong here? Is binding of a wildcard path unsupported in Azure Functions?
I do realize that I could just accept that the routing "works" in the sense that my function is invoked without a binding for the wildcard segment of the route; and do that binding myself in code, but if there is a simpler way to get this to work that would be really useful.
答案 0 :(得分:0)
我知道现在已经太晚了,但是如果我找到了自己的路,那么其他人也可能会。在功能v2中,可以使用参数定义路线。例如:
public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "GET", Route="odata/{category:alpha}/{product:alpha}"]HttpRequest request, ILogger log, string category, string product)
{
log.Log(LogLevel.Information, $"Request: {category}:{product}?{string.Join(",", request.QueryString)}")
}
这些文章在https://jonathangiles.net/creating-custom-routes-in-azure-functions/和此处:https://docs.microsoft.com/en-us/sandbox/functions-recipes/routes?tabs=csharp
中有更多详细信息