c# odata 无服务器函数(隔离模型)需要在根返回一个清单。我在 host.json 文件中将 api 前缀替换为“”,但是当我尝试 Route="" 时,应用程序在根目录返回标准的“您的应用程序已准备就绪”响应。我怎样才能覆盖这种行为?
您可以使用
Route="{ignored:maxlength(0)?}"
在根端点返回响应。
感谢您的见解bredd。
我创建了一个 C# OData 隔离的 Azure 函数,并且能够在根上返回响应。
函数.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;
}
程序.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();
}
主机.json:
"extensions": {
"http": {
"routePrefix": ""
}
}
控制台输出:
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)
回复: