Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用HttpTrigger属性在Azure函数中自定义路由_C#_Function_Azure - Fatal编程技术网

C# 使用HttpTrigger属性在Azure函数中自定义路由

C# 使用HttpTrigger属性在Azure函数中自定义路由,c#,function,azure,C#,Function,Azure,我有一个已编译的Azure函数,我想使用HttpTrigger属性定义一个自定义路由 我的代码如下所示: public static async Task<HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "qotd/{format}")]HttpRequestMessage req, string format, TraceWriter log) {

我有一个已编译的Azure函数,我想使用HttpTrigger属性定义一个自定义路由

我的代码如下所示:

public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "qotd/{format}")]HttpRequestMessage req, string format, TraceWriter log)
{
  var quote = GetRandomQuote();
  if (format == "json")
  {
     return req.CreateResponse(HttpStatusCode.OK, quote, "application/json");
  }
  else
  {
     var strQuote = quote.Text + Environment.NewLine + quote.Author;
     return req.CreateResponse(HttpStatusCode.OK, strQuote, "text/plain");
  }
}
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=“qotd/{format}”)]HttpRequestMessage请求,字符串格式,TraceWriter日志)
{
var quote=GetRandomQuote();
如果(格式==“json”)
{
return req.CreateResponse(HttpStatusCode.OK,引号为“application/json”);
}
其他的
{
var strQuote=quote.Text+Environment.NewLine+quote.Author;
返回请求CreateResponse(HttpStatusCode.OK,strQuote,“文本/普通”);
}
}
当我这样称呼它时: localhost:7071/api/qotd/json 我得到404

当我这样称呼它时: localhost:7071/api/qotd/?format=json 然后函数成功

如果我这样称呼它: localhost:7071/api/qotd/ 我犯了一个相当严重的错误:

“执行函数时出现异常:QotDFunction->异常绑定 参数'format'->绑定数据不包含预期值 “格式…”

如何在HttpTrigger的Route参数中定义绑定,以便像这样调用我的函数:

localhost:7071/api/qotd-用于参数格式的默认值

localhost:7071/api/qotd/json-是否将“json”作为格式值传递


对于路由,我也尝试了“qotd/{format:alpha?}”,但得到了相同的结果。

我测试了您的代码,没有得到与您相同的错误

我创建了一个新的azure函数,它的默认Microsoft.NET.Sdk.Functions版本为1.0.2。我使用Azure.Functiuons.Cli的1.0.7版来运行该函数。我的项目目标框架是.NETFramework 4.6.1

我使用的代码如下所示:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function2")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "qotd/{format:alpha?}")]HttpRequestMessage req, string format, TraceWriter log)
        {

            if (format == "json")
            {
                return req.CreateResponse(HttpStatusCode.OK, "aaa", "application/json");
            }
            else
            {
                return req.CreateResponse(HttpStatusCode.OK, "aaa", "text/plain");
            }
        }
    }
}
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.Azure.WebJobs.Host;
Net系统;
使用System.Net.Http;
使用System.Threading.Tasks;
命名空间函数PP1
{
公共静态类函数1
{
[功能名称(“功能2”)]
公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=“qotd/{format:alpha?}”)]HttpRequestMessage请求,字符串格式,TraceWriter日志)
{
如果(格式==“json”)
{
返回req.CreateResponse(HttpStatusCode.OK,“aaa”,“应用程序/json”);
}
其他的
{
返回请求CreateResponse(HttpStatusCode.OK,“aaa”,“文本/普通”);
}
}
}
}
当我调用函数如
http://localhost:7071/api/qotd
,它将“文本”作为格式值传递

当我调用函数如
http://localhost:7071/api/qotd/json
,它将“json”作为值格式传递。

事实证明,我必须明确地重建项目。重建它迫使VS创建一个具有正确路由的新function.json文件。如果不这样做,function.json仍然保留旧的默认路由