<commit_before>### Introduction

**OData** is defined as "An **open protocol** to allow the creation and
consumption of **queryable** and **interoperable RESTful APIs** in a
**simple** and **standard** way" ([odata.org](http://www.odata.org/)).
You can use OData with ASP.NET Boilerplate. The
[Abp.AspNetCore.OData](https://www.nuget.org/packages/Abp.AspNetCore.OData)
NuGet package simplifies its usage.

### Setup

#### Install NuGet Package

We must first install the Abp.AspNetCore.OData NuGet package to our Web.Core
project:

    Install-Package Abp.AspNetCore.OData

#### Set Module Dependency

We need to set a dependency on AbpAspNetCoreODataModule for our module.
Example:

    [DependsOn(typeof(AbpAspNetCoreODataModule))]
    public class MyProjectWebCoreModule : AbpModule
    {
        ...
    }

See the [module system](/Pages/Documents/Module-System) to understand module
dependencies.

#### Configure Your Entities

OData requires us to declare entities which can be used as OData resources.
We must do this in the Startup class:

    public class Startup
    {
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            ...

            services.AddOData();

            // Workaround: https://github.com/OData/WebApi/issues/1177
            services.AddMvcCore(options =>
            {
                foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
                {
                    outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
                foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
                {
                    inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
            });

            return services.AddAbp<MyProjectWebHostModule>(...);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseAbp();

            ...

            app.UseOData(builder =>
            {
                builder.EntitySet<Person>("Persons").EntityType.Expand().Filter().OrderBy().Page();
            });

            // Return IQueryable from controllers
            app.UseUnitOfWork(options =>
            {
                options.Filter = httpContext =>
                {
                    return httpContext.Request.Path.Value.StartsWith("/odata");
                };
            });

            app.UseMvc(routes =>
            {
                routes.MapODataServiceRoute(app);

                ...
            });
        }
    }

Here, we got the ODataModelBuilder reference and set the Person entity.
You can use EntitySet to add other entities in a similar way. See the [OData
documentation](http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint)
for more information on the builder.

### Create Controllers

The Abp.AspNetCore.OData NuGet package includes the **AbpODataEntityController**
base class (which extends standard ODataController) to create your
controllers easier. An example to create an OData endpoint for the Person
entity:

    public class PersonsController : AbpODataEntityController<Person>, ITransientDependency 
    {
        public PersonsController(IRepository<Person> repository)
            : base(repository)
        {<commit_msg>Update OData-AspNetCore-Integration.md<commit_after>### Introduction

**OData** is defined as "An **open protocol** to allow the creation and
consumption of **queryable** and **interoperable RESTful APIs** in a
**simple** and **standard** way" ([odata.org](http://www.odata.org/)).
You can use OData with ASP.NET Boilerplate. The
[Abp.AspNetCore.OData](https://www.nuget.org/packages/Abp.AspNetCore.OData)
NuGet package simplifies its usage.

### Setup

#### Install NuGet Package

We must first install the Abp.AspNetCore.OData NuGet package to our Web.Core
project:

    Install-Package Abp.AspNetCore.OData

#### Set Module Dependency

We need to set a dependency on AbpAspNetCoreODataModule for our module.
Example:

    [DependsOn(typeof(AbpAspNetCoreODataModule))]
    public class MyProjectWebCoreModule : AbpModule
    {
        ...
    }

See the [module system](/Pages/Documents/Module-System) to understand module
dependencies.

#### Configure Your Entities

OData requires us to declare entities which can be used as OData resources.
We must do this in the Startup class:

##### For asp net core 2.x

    public class Startup
    {
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            ...

            services.AddOData();

            // Workaround: https://github.com/OData/WebApi/issues/1177
            services.AddMvcCore(options =>
            {
                foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
                {
                    outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
                foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
                {
                    inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
            });

            return services.AddAbp<MyProjectWebHostModule>(...);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseAbp();

            ...

            app.UseOData(builder =>
            {
                builder.EntitySet<Person>("Persons").EntityType.Expand().Filter().OrderBy().Page();
            });

            // Return IQueryable from controllers
            app.UseUnitOfWork(options =>
            {
                options.Filter = httpContext =>
                {
                    return httpContext.Request.Path.Value.StartsWith("/odata");
                };
            });

            app.UseMvc(routes =>
            {
                routes.MapODataServiceRoute(app);

                ...
            });
        }
    }

##### For asp net core 3.x

    public class Startup
    {
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            ...

			services.AddOData();

			// Workaround: https://github.com/OData/WebApi/issues/1177
			services.AddMvcCore(options =>
			{
				foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
				{
					outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
				}
				foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
				{
					inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
				}
			});

            return services.AddAbp<MyProjectWebHostModule>(...);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseAbp();

            ...

			
			// Return IQueryable from controllers
			app.UseUnitOfWork(options =>
			{
				options.Filter = httpContext => httpContext.Request.Path.Value.StartsWith("/odata");
			});

			app.UseODataBatching();
			app.UseEndpoints(endpoints =>
			{
				...	

				var builder = new ODataConventionModelBuilder();
				builder.EntitySet<Person>("Persons").EntityType.Expand().Filter().OrderBy().Page();
				endpoints.MapODataRoute("odataPrefix", "odata",  builder.GetEdmModel());
				...
			});
			
			...
        }
    }


Here, we got the ODataModelBuilder reference and set the Person entity.
You can use EntitySet to add other entities in a similar way. See the [OData
documentation](http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint)
for more information on the builder.

### Create Controllers

The Abp.AspNetCore.OData NuGet package includes the **AbpODataEntityController**
base class (which extends standard ODataController) to create your
controllers easier. An example to create an OData endpoint for the Person
entity:

    public class PersonsController : AbpODataEntityController<Person>, ITransientDependency 
    {
        public PersonsController(IRepository<Person> repository)
            : base(repository)
        {
{'fraction_non_alphanumeric': 0.10419026047565119, 'fraction_numerical': 0.0027180067950169874, 'mean_word_length': 2.546586345381526, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 39, 'https://': 5, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6877634', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2358, 'n_words': 545}