Azure Webjobs SDK: HttpTrigger. What is the default URL on localhost?

Viewed 599

I have this function


    public class Functions
    {
        public Functions()
        {

        }

        [FunctionName("httptriggertest")]
        public async Task<IActionResult> HttpTriggerTest([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "httptriggertest")] HttpRequest req,
            ILogger log)
        {
            log.Log(LogLevel.Information, "Received request request");

            return new OkObjectResult("HttpTriggerTest");
        }
    }

And run it by this:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebJobs(b =>
                {
                    b.AddAzureStorageCoreServices()
                        .AddAzureStorage()
                        .AddHttp();
                })
                .ConfigureLogging((context, b) =>
                {
                    b.SetMinimumLevel(LogLevel.Debug);
                    b.AddConsole();
                })
                .ConfigureAppConfiguration(b =>
                {
                    b.AddCommandLine(args);
                })
                .UseConsoleLifetime();
    }

when i run dotnet run it starts fine (the function is discovered by the runtime)

info: Microsoft.Azure.WebJobs.Hosting.JobHostService[0]
      Starting JobHost
info: Host.Startup[0]
      Found the following functions:
      Functions.Functions.HttpTriggerTest

info: Host.Startup[0]
      Job host started
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /home/mslot/git/sut_test/job1
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
      Hosting started
^Cinfo: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...
dbug: Microsoft.Extensions.Hosting.Internal.Host[3]
      Hosting stopping
info: Microsoft.Azure.WebJobs.Hosting.JobHostService[0]
      Stopping JobHost
info: Host.Startup[0]
      Stopping the listener 'Microsoft.Azure.WebJobs.Extensions.Http.HttpTriggerAttributeBindingProvider+HttpTriggerBinding+NullListener' for function 'httptriggertest'
info: Host.Startup[0]
      Stopped the listener 'Microsoft.Azure.WebJobs.Extensions.Http.HttpTriggerAttributeBindingProvider+HttpTriggerBinding+NullListener' for function 'httptriggertest'
info: Host.Startup[0]
      Job host stopped
dbug: Microsoft.Extensions.Hosting.Internal.Host[4]
      Hosting stopped

But what is the URL for this? Is this even possible on localhost to call the http endpoint? I have tried all sorts of variations of http://localhost/

1 Answers

You can get the URL from HttpTrigger route():

route():

  • route() defines the route template, controlling which request URLs your function will respond to. The default value if no route is provided is the function name specified in the FunctionName annotation, applied to each Azure Function.

  • By default when you create a function for an HTTP trigger, or WebHook, the function is addressable with a route of the form http://<yourapp>.azurewebsites.net/api/<funcname>. You can customize this route using this route property. For example, a route of "products/{category:alpha}/{id:int}" would mean that the function is now addressable with the following route instead of the original route: http://<yourapp>.azurewebsites.net/api/products/electronics/357, which allows the function code to support two parameters in the address: category and id.

For example:

[FunctionName("GetCounter")]
public static async Task<HttpResponseMessage> GetCounter(
    [HttpTrigger(AuthorizationLevel.Function, "get", Route = "Counter/{entityKey}")] HttpRequestMessage req,
    [DurableClient] IDurableEntityClient client,
    string entityKey)
{
    var entityId = new EntityId("Counter", entityKey);
    var state = await client.ReadEntityStateAsync<Counter>(entityId); 
    return req.CreateResponse(state);
}

You can refer to open GitHub issue at HTTP Trigger Binding Extensibility and durable entities in .NET

Related