使用参数创建azure无服务器功能

时间:2018-06-11 15:10:02

标签: c# azure-functions

我正在尝试创建一个简单的函数,它将获取get参数并根据该参数返回类别列表。

但是,我找不到任何有关路由或传递参数的有用文档,所以这就是我所拥有的 - 我只需要一个如何访问网站参数的指针(如果路由正确)

public static class GetCategoriesCRUDFunction
{
    [FunctionName("CategoriesFunction")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "{website}")]HttpRequestMessage request, TraceWriter log) // is this how to set up a route or do I need to do attribute routing?
    {
        IService service = new Service();

        var categories = await service.GetCategories(website);  // how do I get this website parameter from the querystring - do I need to use something like httpcontext?

        return request.CreateResponse(HttpStatusCode.OK, categories);
    }
}

1 个答案:

答案 0 :(得分:5)

你几乎就在那里。要实际从查询字符串中获取值,请将参数添加到Run方法:

[FunctionName("CategoriesFunction")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", Route = "{website}")] HttpRequestMessage request, 
    string website, 
    TraceWriter log)

您可能希望在路由中添加内容...默认情况下,所有HttpTriggered函数都具有api的路由前缀。例如,您可以将路线设为Route = "categories/{website}",以便网址为https://<function_app_url>/api/categories/<websitename>