简体   繁体   English

ASP.net核心OData字符串键为空

[英]ASP.net Core OData String Key Is Null

I have a strange one here. 我这里有一个奇怪的人。 I've been porting some controllers over from asp.net odata to asp.net core odata and ran into a bit of a snag with the primary keys. 我一直在将一些控制器从asp.net odata移植到asp.net核心odata,并遇到了一些与主键有关的问题。

In my .net framework 4.6.2 app I have a GUID as a primary key and in the .net core app I have a string as a primary key. 在我的.net Framework 4.6.2应用程序中,我有一个GUID作为主键,而在.net核心应用程序中,我有一个字符串作为主键。 I've been able to get most everything working EXCEPT a Get(key) method. 除了Get(key)方法之外,我已经能够使大多数工作正常。 This is my method signature: 这是我的方法签名:

[HttpGet]
[EnableQuery]
public async Task<IActionResult> Get([FromODataUri] string key)
{
    // key is null!
}

Please Below Follow step 请在下面按照步骤

  1. Install Microsoft.AspNetCore.OData from NuGet Package 从NuGet包安装Microsoft.AspNetCore.OData
  2. StartUp.cs 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 Api Conteroller 在Api控制器中

     [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; } } 
  4. After Run Api And Check Like This 运行Api之后并像这样检查 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM