我有工作的ODATA应用程序服务,我目前正在使用它来获得Excel和Power BI的数据,但是我想在Azure函数应用程序v4中实现ODATA,但我无法在function应用程序中获得响应应用程序(no @odata) 。
有一种方法使ODATA在功能应用程序中工作我想要函数app
功能的正确odata响应
I使用Runtime Stack .NET 8.0创建了HTTP触发函数,并在功能中配置了ODATA。
以下功能代码成功工作。
功能代码:
public class Function1
{
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function("GetProducts")]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "products")] HttpRequest req)
{
// Log information for debugging purposes
_logger.LogInformation("C# HTTP trigger function processed a request.");
// Sample list of products (can be replaced with data from a DB like Cosmos DB or SQL)
var data = new List<Product>
{
new Product() { Title = "Mountain Bike SERIOUS ROCKVILLE", Category = "Mountain Bicycle" },
new Product() { Title = "Mountain Bike eléctrica HAIBIKE SDURO HARD SEVEN", Category = "Mountain Bicycle" },
new Product() { Title = "Sillín BROOKS CAMBIUM C15 CARVED ALL WEATHER", Category = "Sillin" },
new Product() { Title = "Poncho VAUDE COVERO II Amarillo", Category = "Chaquetas" },
};
// Apply OData query options (like $filter, $orderby, etc.)
var queryOptions = req.Query;
var filteredData = ApplyODataQueryOptions(data.AsQueryable(), queryOptions);
// Prepare the OData response (with metadata)
var response = new ODataResponse
{
Context = "https://yourfunctionapp.azurewebsites.net/odata/$metadata#products", // Adjust this URL
Count = filteredData.Count(),
Value = filteredData.ToList()
};
// Return the OData response as JSON
return new JsonResult(response)
{
StatusCode = 200
};
}
// Helper function to apply OData query options like $filter, $orderby, etc.
private IQueryable<Product> ApplyODataQueryOptions(IQueryable<Product> data, IQueryCollection queryOptions)
{
if (queryOptions.ContainsKey("$filter"))
{
// Apply filtering logic (basic for illustration; you can expand this)
var filterValue = queryOptions["$filter"].ToString(); // Convert StringValues to a string
if (filterValue.Contains("Category eq"))
{
// Extract the category value directly (without using Split)
var categoryIndex = filterValue.IndexOf("Category eq") + "Category eq".Length;
var category = filterValue.Substring(categoryIndex).Trim(); // Extract the category part
// Clean the value by removing quotes
if (category.StartsWith("'") && category.EndsWith("'"))
{
category = category.Substring(1, category.Length - 2); // Remove single quotes
}
// Apply filter based on the category
data = data.Where(p => p.Category == category);
}
}
if (queryOptions.ContainsKey("$orderby"))
{
var orderbyValue = queryOptions["$orderby"].ToString(); // Convert StringValues to a string
if (orderbyValue.Contains("Title"))
{
data = data.OrderBy(p => p.Title);
}
}
return data;
}
// OData response format
public class ODataResponse
{
public string? Context { get; set; }
public int Count { get; set; } // Total count of results
public IEnumerable<Product>? Value { get; set; } // Actual data
}
// Define the Product class directly here (or can be in a separate file if preferred)
public class Product
{
public string? Title { get; set; }
public string? Category { get; set; }
}
}
函数的状态下面的状态:
输出: