locked
ASP.net Core OData user without change In Get Api Parameter RRS feed

  • Question

  • User105007481 posted

    My GET API In Not change or not added Parameter but Directly Use With FILTER or SELECT Using Odata.

    [HttpGet]
    public async Task<IActionResult> Get()
    {
       //Some Of Code
    }

    Wednesday, March 27, 2019 6:20 AM

Answers

  • User-1038772411 posted

    Please Below Follow step

    1. Install Microsoft.AspNetCore.OData from NuGet Package
    2. StartUp.cs

      public void ConfigureServices(IServiceCollection services)
         {
           services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
           services.AddOData(); //This Is added for OData
         }
      
       public void Configure(IApplicationBuilder app, IHostingEnvironment env)
       {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          else
          {
              app.UseHsts();
          }
      
          app.UseHttpsRedirection();
          app.UseMvc(routeBuilder =>
          {
              routeBuilder.EnableDependencyInjection(); //This Is added for OData
              routeBuilder.Expand().Select().Count().OrderBy().Filter(); //This Is added for OData
          });
      }

    3. In <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="9" data-gr-id="9">Api</g> <g class="gr_ gr_10 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="10" data-gr-id="10">Conteroller</g>
    [HttpGet]
    [EnableQuery] //This Is added for OData
    public ActionResult<List<TestModel>> Get()
    {
        var model = new List<TestModel>();
        for (int i = 1; i <= 10; i++)
        {
            var res = new TestModel()
            {
                ID = i,
                Name="Test"+i,
                Mobile="Test"+i,
                City="Test_"+i
            };
            model.Add(res);
        }
        return model;
    }
    public class TestModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public string City { get; set; }
    }

    For Your Example Given This Get API

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 27, 2019 7:18 AM
  • User-1038772411 posted

    Please refer this link may be this will help you For OData

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 27, 2019 7:30 AM

All replies

  • User-1038772411 posted

    Please Below Follow step

    1. Install Microsoft.AspNetCore.OData from NuGet Package
    2. StartUp.cs

      public void ConfigureServices(IServiceCollection services)
         {
           services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
           services.AddOData(); //This Is added for OData
         }
      
       public void Configure(IApplicationBuilder app, IHostingEnvironment env)
       {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          else
          {
              app.UseHsts();
          }
      
          app.UseHttpsRedirection();
          app.UseMvc(routeBuilder =>
          {
              routeBuilder.EnableDependencyInjection(); //This Is added for OData
              routeBuilder.Expand().Select().Count().OrderBy().Filter(); //This Is added for OData
          });
      }

    3. In <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="9" data-gr-id="9">Api</g> <g class="gr_ gr_10 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="10" data-gr-id="10">Conteroller</g>
    [HttpGet]
    [EnableQuery] //This Is added for OData
    public ActionResult<List<TestModel>> Get()
    {
        var model = new List<TestModel>();
        for (int i = 1; i <= 10; i++)
        {
            var res = new TestModel()
            {
                ID = i,
                Name="Test"+i,
                Mobile="Test"+i,
                City="Test_"+i
            };
            model.Add(res);
        }
        return model;
    }
    public class TestModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public string City { get; set; }
    }

    For Your Example Given This Get API

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 27, 2019 7:18 AM
  • User-1038772411 posted

    Please refer this link may be this will help you For OData

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 27, 2019 7:30 AM