Hcs.Extensions.OdataClient
1.0.3
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Hcs.Extensions.OdataClient --version 1.0.3
NuGet\Install-Package Hcs.Extensions.OdataClient -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hcs.Extensions.OdataClient" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hcs.Extensions.OdataClient" Version="1.0.3" />
<PackageReference Include="Hcs.Extensions.OdataClient" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hcs.Extensions.OdataClient --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hcs.Extensions.OdataClient, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Hcs.Extensions.OdataClient@1.0.3
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Hcs.Extensions.OdataClient&version=1.0.3
#tool nuget:?package=Hcs.Extensions.OdataClient&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Hcs.Extensions.OdataClient
C# simple odata client with no $metadata needed for someone only use OdataQueryOptions for Query api like me
install package
Install-Package Hcs.Extensions.OdataClient
use
var httpClient = new HttpClient { BaseAddress = new Uri("https://localhost:44326/") };
var maxId = 100;
var req = httpClient.GetOdataClient<File>("/api/FileManage")
.Where(x => x.CategoryId.HasValue)
.Where(x => x.CategoryId.HasValue && x.Category.Id + 10 < maxId)
.OrderBy(x => x.Path).ThenBy(x => x.Name)
.Take(100)
.Select(x => new { brand_new_id_property = x.Id, x.Path, xxxx = new { x.Category.Name } });
Console.WriteLine(req.GetQueryString(encode: false));
foreach (var result in await req.SendReqeust())
{
Console.WriteLine($"{result.brand_new_id_property} {result.xxxx.Name}");
}
excute result
?$filter=CategoryId ne null and (CategoryId ne null and (Category/Id add 10) lt 100)&$orderby=Path asc,Name asc&$top=100&$count=true&$select=Id,Path&$expand=Category($select=Name)
3 A
2 A
4 A
5 A
server side configuration for .net core
install package
Microsoft.AspNetCore.OData
add services
services.AddOData();
services.AddODataQueryFilter();
services.AddSingleton<ODataUriResolver, StringAsEnumResolver>();
//for .net core 3.1 you need replace built in json serializer for odata $select/$expand working
services.AddControllersWithViews().AddNewtonsoftJson(options =>
{
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
options.SerializerSettings.StringEscapeHandling = Newtonsoft.Json.StringEscapeHandling.EscapeHtml;
});
endpoint
app.UseEndpoints(endpoints =>
{
endpoints.EnableDependencyInjectionForOdata();
endpoints.Count().Filter().OrderBy().Expand().Select().MaxTop(100); //allow complex query function
// ....
});
controller
[Route("api/[controller]")]
[ApiController]
public class FileManageController : ControllerBase
{
DbContext context;
public FileManageController(DbContext context)
{
this.context = context;
}
[EnableQuery]
public IActionResult Get()
{
return Ok(context.Set<File>().AsQueryable());
}
}
count
you can replace odata's EnableQueryAttribute to custom ActionFilter like this
public class HcsEnableQueryAttribute : EnableQueryAttribute
{
public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
var odata = actionExecutedContext.HttpContext.ODataFeature();
if (odata.TotalCount.HasValue)
{
actionExecutedContext.HttpContext.Response.Headers.Add("x-total-count", odata.TotalCount.Value.ToString("#0"));
}
}
}
and read count from response header
var response = await req.SendReqeust();
if (response.HttpResponse.Headers.TryGetValues("x-total-count", out IEnumerable<string> values)
&& values.Any()
&& int.TryParse(values.First(), out int count))
{
// do things...
}
you can create an extension methodfor this
WARNING
I wrote this lib in rush (about 20 hours),so use with your own risk, feel free to file a PR if you encounter any bugs
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- System.Text.Json (>= 4.7.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.