HttpTrigger 在 Azure 函数中不起作用
HttpTrigger not working in Azure Function
我正在玩弄 Azure Function 中的触发器
我有一个函数需要 2 个操作 - 一个是通过服务总线更改调用的,因此我使用 ServiceBusTrigger,另一个是通过 HttpTrigger
我已经通过调试设置将功能设置为使用端口 8085
public static class MyFunction
{
[FunctionName("MyFunction")]
public static void Run([ServiceBusTrigger("My-topic", "MySubscription", Connection = "eventbus-connection")]string mySbMsg, ILogger log)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
var payment = JsonConvert.DeserializeObject<MyObject>(mySbMsg);
}
public static async Task<IActionResult> TestMethodAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "api/payments/test-method")]
HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = String.Empty;
using (StreamReader streamReader = new StreamReader(req.Body))
{
requestBody = await streamReader.ReadToEndAsync();
}
dynamic data = JsonConvert.DeserializeObject(requestBody);
name ??= data?.name;
return name != null
? (ActionResult) new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
服务总线触发器工作正常,但是,当我尝试调用 Http 端点时总是收到 404 错误
我试过了
http://localhost:8085/api/payments/test-method?name=2
http://localhost:8085/api/payments/test-method?name=2
http://localhost:8085/api/payments/test-method?name=2
http://localhost:8085/payments/test-method?name=2
http://localhost:8085/payments/test-method?name=2
http://localhost:8085/payments/test-method?name=2
None 这些作品
我做错了什么?我知道使用 Anonymous 作为安全性是错误的,但我想先让它为基本的工作,然后我需要看看如何将功能键放入请求中
保罗
如果不设置Route
的值,函数url应该是http://localhost:8085/api/MyFunction
(HttpTrigger操作需要加一个FunctionName
)。
在你的例子中,因为你已经设置了 Route
的值。所以你的函数url应该是:http://localhost:8085/api/api/payments/test-method
(注意有两个/api
)。
如果您希望 url 为 http://localhost:8085/api/payments/test-method
,请设置 Route = "payments/test-method"
我正在玩弄 Azure Function 中的触发器
我有一个函数需要 2 个操作 - 一个是通过服务总线更改调用的,因此我使用 ServiceBusTrigger,另一个是通过 HttpTrigger
我已经通过调试设置将功能设置为使用端口 8085
public static class MyFunction
{
[FunctionName("MyFunction")]
public static void Run([ServiceBusTrigger("My-topic", "MySubscription", Connection = "eventbus-connection")]string mySbMsg, ILogger log)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
var payment = JsonConvert.DeserializeObject<MyObject>(mySbMsg);
}
public static async Task<IActionResult> TestMethodAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "api/payments/test-method")]
HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = String.Empty;
using (StreamReader streamReader = new StreamReader(req.Body))
{
requestBody = await streamReader.ReadToEndAsync();
}
dynamic data = JsonConvert.DeserializeObject(requestBody);
name ??= data?.name;
return name != null
? (ActionResult) new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
服务总线触发器工作正常,但是,当我尝试调用 Http 端点时总是收到 404 错误
我试过了
http://localhost:8085/api/payments/test-method?name=2
http://localhost:8085/api/payments/test-method?name=2
http://localhost:8085/api/payments/test-method?name=2
http://localhost:8085/payments/test-method?name=2
http://localhost:8085/payments/test-method?name=2
http://localhost:8085/payments/test-method?name=2
None 这些作品
我做错了什么?我知道使用 Anonymous 作为安全性是错误的,但我想先让它为基本的工作,然后我需要看看如何将功能键放入请求中
保罗
如果不设置Route
的值,函数url应该是http://localhost:8085/api/MyFunction
(HttpTrigger操作需要加一个FunctionName
)。
在你的例子中,因为你已经设置了 Route
的值。所以你的函数url应该是:http://localhost:8085/api/api/payments/test-method
(注意有两个/api
)。
如果您希望 url 为 http://localhost:8085/api/payments/test-method
,请设置 Route = "payments/test-method"