Search code examples
c#.netazureserverless

How can an azure v4 serverless function return a response on the root?


A c# odata serverless function (isolated model) needs to return a manifest at the root. I replaced the api prefix with "" in the host.json file but when I try Route="" the application returns the standard 'your application is ready' response at the root. How can I override this behaviour?


Solution

  • You can use Route="{ignored:maxlength(0)?}" to return the response at the root endpoint.

    Thanks for the insights bredd.

    I have created a C# OData isolated Azure function and able to return the response on the root.

    function.cs:

    [Function("GetProducts")]
    public async Task<HttpResponseData> GetProducts([HttpTrigger(AuthorizationLevel.Function, "get","post", Route = "{ignored:maxlength(0)?}")] HttpRequestData req, string ignored = "")
    {
    
    var response = req.CreateResponse(HttpStatusCode.OK);
        var manifest = new
        {
            name = "My OData Service",
            version = "1.0.0",
            description = "This is the manifest for my OData service."
        };
    
        await response.WriteAsJsonAsync(manifest);
        return response;
    }
    

    Program.cs:

    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication()
       .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices(services =>
        {
            services.AddControllers()
                .AddOData(options => options.Select()
           .Filter()
           .OrderBy()
           .Count()
           .Expand()
           .SetMaxTop(100)
           .AddRouteComponents("odata", GetEdmModel()));
        })
        .Build();
    
    host.Run();
    IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<Products>("Products");
        return builder.GetEdmModel();
    }
    

    host.json:

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

    Console Output:

    Functions:
    
            GetProducts: [GET,POST] http://localhost:7220/{ignored:maxlength(0)?}
    
    For detailed output, run func with --verbose flag.
    [2025-01-03T13:45:16.568Z] Executing 'Functions.GetProducts' (Reason='This function was programmatically called via the host APIs.', Id=d72799f9-72f5-49c1-8f3e-95fb7461d930)
    [2025-01-03T13:45:18.335Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
    [2025-01-03T13:45:21.906Z] Executed 'Functions.GetProducts' (Succeeded, Id=d72799f9-72f5-49c1-8f3e-95fb7461d930, Duration=5418ms)
    

    Response:

    enter image description here