Dela via


HTTP routing

Controlling the route prefix with host.json

By default Azure Functions has a route prefix on all functions /api/. To change this, access the routePrefix property in the http field of host.json. To remove it completely, leave the field blank.

{
  "http": {
    "routePrefix": ""
  }
}

This would turn http://yourUrl/api/Example into http://yourUrl/Example.

Takeaways

  • Route prefixes are customizable and removable using host.json.

Read more

Define the function route with function properties

There are several ways to define function routes. One of those ways is in the function header.

[FunctionName("Example")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route="Example")]HttpRequestMessage req,
                                                   TraceWriter log)
{
    log.Info($"C# HTTP trigger function processed a request");

    return new HttpResponseMessage(HttpStatusCode.Accepted);
}

Define the function route in the Azure portal

Routes can also be controlled from the integrate tab of your function in the Azure portal.

Enter the route that you want your function to have in the Route template box on the page.

Takeaways

  • The Azure portal allows you to change your routes in the Integrate tab.
  • If you are using continuous deployment for your functions, any settings changed will be reset on redeployment.

Read more

Adding parameters to function routes

To add parameters to your route, put the parameter in curly braces in the route property of the HttpTrigger attribute, and add it in the method parameters.

[FunctionName("Example")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route="Example/{parameter}")]HttpRequestMessage req,
                                                   string parameter,
                                                   TraceWriter log)
{
    log.Info($"C# HTTP trigger function processed a request {parameter}");

    return new HttpResponseMessage(HttpStatusCode.Accepted);
}

Making route parameters optional

In order to make route parameters optional in your function call, add a ? after the parameter name in the route definition and the type in the function header.

[FunctionName("Example")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route="Example/{parameter?}")]HttpRequestMessage req,
                                                  string parameter,
                                                  TraceWriter log)
{
    log.Info($"C# HTTP trigger function processed a request {parameter}");

    return new HttpResponseMessage(HttpStatusCode.Accepted);
}