OData 中的属性路由功能 Web API Core 2.2
Attribute Routing in OData featured Web API Core 2.2
我正在开发使用 OData(开放数据协议)7.2.1 版的 ASP.NET Core 2.2 Web API。
它与传统的工作正常 routing.I 想在操作方法中添加属性路由以便我可以重载 methods.But 没有成功。
我在 OData 网站上没有找到任何适合我的文档。这是我的代码。
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OData.Edm;
using ODataService.Model;
using System.Linq;
namespace ODataService
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//Adding In Memory Database.
services.AddDbContext<SampleODataDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDb");
});
//Adding OData middleware.
services.AddOData();
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//app.UseMvc();
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Person>(nameof(Person));
//Enabling OData routing/web api routing
app.UseMvc(routeBuilder =>
{
routeBuilder.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routeBuilder.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata",
model: builder.GetEdmModel()
);
routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
//routeBuilder.EnableDependencyInjection();
});
}
}
}
//Here is controller class
using System;
using System.Linq;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using ODataService.Model;
using Microsoft.AspNet.OData.Routing;
using System.Threading.Tasks;
namespace ODataService.Controllers
{
[Route("api/[controller]")]
//[ODataRouting()]
[ODataRoutePrefix("Person")]
[ApiController]
public class PersonController : ODataController
{
private readonly SampleODataDbContext _appDbContext;
public PersonController(SampleODataDbContext sampleODataDbContext)
{
_appDbContext = sampleODataDbContext;
}
//[ODataRoute("GetAllPerson")]
[EnableQuery(AllowedArithmeticOperators =Microsoft.AspNet.OData.Query.AllowedArithmeticOperators.None)]
public IActionResult Get()
{
var NewPerson1 = new Person
{
Age = 20,
Id = new Guid(),
Name = "test"
};
Person NewPerson2 = new Person
{
Age = 25,
Id = new Guid(),
Name = "test jai"
};
_appDbContext.Persons.Add(NewPerson1);
_appDbContext.SaveChanges();
_appDbContext.Persons.Add(NewPerson2);
_appDbContext.SaveChanges();
return Ok(_appDbContext.Persons.AsQueryable());
}
//[ODataRoute("GetData")]
[EnableQuery(AllowedArithmeticOperators = Microsoft.AspNet.OData.Query.AllowedArithmeticOperators.None)]
public IActionResult GetData()
{
var NewPerson1 = new Person
{
Age = 20,
Id = new Guid(),
Name = "abcd efgh"
};
_appDbContext.Persons.Add(NewPerson1);
_appDbContext.SaveChanges();
return Ok(_appDbContext.Persons.AsQueryable());
}
[Route("GetAllEmployee")]
public IActionResult GetAll()
{
Person NewPerson1 = new Person
{
Age = 20,
Id = new Guid(),
Name = "test"
};
Person NewPerson2 = new Person
{
Age = 25,
Id = new Guid(),
Name = "test jai"
};
_appDbContext.Persons.Add(NewPerson1);
_appDbContext.SaveChanges();
_appDbContext.Persons.Add(NewPerson2);
_appDbContext.SaveChanges();
return Ok(_appDbContext.Persons.AsQueryable());
}
}
}
我想执行下面给定的两个 URL
http://localhost:5000/OData/Person/GetData
http://localhost:5000/OData/Person
有什么建议吗?
经过长时间使用属性路由、函数和操作的实验,可以使用 OData 函数实现上述目标。
下面是它的代码。
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Person>(nameof(Person)).EntityType.Collection.Function("GetData").Returns<Person>();
请参阅以下内容以获得更多理解。
我正在开发使用 OData(开放数据协议)7.2.1 版的 ASP.NET Core 2.2 Web API。
它与传统的工作正常 routing.I 想在操作方法中添加属性路由以便我可以重载 methods.But 没有成功。
我在 OData 网站上没有找到任何适合我的文档。这是我的代码。
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OData.Edm;
using ODataService.Model;
using System.Linq;
namespace ODataService
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//Adding In Memory Database.
services.AddDbContext<SampleODataDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDb");
});
//Adding OData middleware.
services.AddOData();
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//app.UseMvc();
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Person>(nameof(Person));
//Enabling OData routing/web api routing
app.UseMvc(routeBuilder =>
{
routeBuilder.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routeBuilder.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata",
model: builder.GetEdmModel()
);
routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
//routeBuilder.EnableDependencyInjection();
});
}
}
}
//Here is controller class
using System;
using System.Linq;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using ODataService.Model;
using Microsoft.AspNet.OData.Routing;
using System.Threading.Tasks;
namespace ODataService.Controllers
{
[Route("api/[controller]")]
//[ODataRouting()]
[ODataRoutePrefix("Person")]
[ApiController]
public class PersonController : ODataController
{
private readonly SampleODataDbContext _appDbContext;
public PersonController(SampleODataDbContext sampleODataDbContext)
{
_appDbContext = sampleODataDbContext;
}
//[ODataRoute("GetAllPerson")]
[EnableQuery(AllowedArithmeticOperators =Microsoft.AspNet.OData.Query.AllowedArithmeticOperators.None)]
public IActionResult Get()
{
var NewPerson1 = new Person
{
Age = 20,
Id = new Guid(),
Name = "test"
};
Person NewPerson2 = new Person
{
Age = 25,
Id = new Guid(),
Name = "test jai"
};
_appDbContext.Persons.Add(NewPerson1);
_appDbContext.SaveChanges();
_appDbContext.Persons.Add(NewPerson2);
_appDbContext.SaveChanges();
return Ok(_appDbContext.Persons.AsQueryable());
}
//[ODataRoute("GetData")]
[EnableQuery(AllowedArithmeticOperators = Microsoft.AspNet.OData.Query.AllowedArithmeticOperators.None)]
public IActionResult GetData()
{
var NewPerson1 = new Person
{
Age = 20,
Id = new Guid(),
Name = "abcd efgh"
};
_appDbContext.Persons.Add(NewPerson1);
_appDbContext.SaveChanges();
return Ok(_appDbContext.Persons.AsQueryable());
}
[Route("GetAllEmployee")]
public IActionResult GetAll()
{
Person NewPerson1 = new Person
{
Age = 20,
Id = new Guid(),
Name = "test"
};
Person NewPerson2 = new Person
{
Age = 25,
Id = new Guid(),
Name = "test jai"
};
_appDbContext.Persons.Add(NewPerson1);
_appDbContext.SaveChanges();
_appDbContext.Persons.Add(NewPerson2);
_appDbContext.SaveChanges();
return Ok(_appDbContext.Persons.AsQueryable());
}
}
}
我想执行下面给定的两个 URL
http://localhost:5000/OData/Person/GetData
http://localhost:5000/OData/Person
有什么建议吗?
经过长时间使用属性路由、函数和操作的实验,可以使用 OData 函数实现上述目标。
下面是它的代码。
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Person>(nameof(Person)).EntityType.Collection.Function("GetData").Returns<Person>();
请参阅以下内容以获得更多理解。