如何为表或存储过程中的show result配置Odata api

时间:2022-12-05 10:05:01

I am going to create an Odata api in asp.net mvc 4 for get data from new table. when I call the Odata method and use debug in the code It shows me data properly. But when it comes to browser, it shows empty screen. There is no error shown in the code.

我将在asp.net mvc 4中创建一个Odata api,用于从新表中获取数据。当我调用Odata方法并在代码中使用debug它正确显示数据。但是当涉及到浏览器时,它会显示空白屏幕。代码中没有显示错误。

this is my Odata method :

这是我的Odata方法:

[Queryable]
    public HCPData GetHCPData([FromODataUri] int key)
    {
      //  return SingleResult.Create(db.HCPDatas.Where(hcpdata => hcpdata.Id == key));
        IQueryable<HCPData> result = db.HCPDatas.Where(p => p.CompanyId == key);
        return result.FirstOrDefault();
    }

this is my WebApiConfig method:

这是我的WebApiConfig方法:

 public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

         ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
        //var entitySetConfiguration1 = modelBuilder.EntitySet<Job>("Job");
         var entitySetConfiguration1 = modelBuilder.EntitySet<HCPData>("HCPData");


         var customer = modelBuilder.EntityType<HCPData>();

         modelBuilder.EntitySet<HCPData>("HCPData");


        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: modelBuilder.GetEdmModel());
    }

When I checked the console of empty screen in browser it shows an error: "NetworkError: 406 Not Acceptable - http://localhost:50369/HCPData?key=11"

当我在浏览器中检查空屏幕控制台时显示错误:“NetworkError:406 Not Acceptable - http:// localhost:50369 / HCPData?key = 11”

Please let me know the solution of the issue. Thanks in advance.

请让我知道问题的解决方案。提前致谢。

1 个解决方案

#1


0  

What's the result if you change the controller as follows:

如果您按如下方式更改控制器,结果如何:

public class HCPDataController : ODataController
{
   [EnableQuery]
   public HCPData GetHCPData([FromODataUri] int key)
   {
    ...
   }
}

[My Sample]

Because, at my side, if I implement the controller as follows, it can work:

因为,在我这边,如果我按如下方式实现控制器,它可以工作:

[EnableQuery]
public HCPData GetHCPData([FromODataUri] int key)
{
     var data = new HCPData
     {
         CompanyId = 2,
         Name = "Key = " + key
     };

     return data;
}

Example:

Let me issue the following request:

让我发出以下请求:

如何为表或存储过程中的show result配置Odata api

I can get the following response:

我可以得到以下回复:

{
  "@odata.context":"http://localhost:62591/odata/$metadata#HCPData/$entity","CompanyId":2,"Name":"Key = 11"
}

#1


0  

What's the result if you change the controller as follows:

如果您按如下方式更改控制器,结果如何:

public class HCPDataController : ODataController
{
   [EnableQuery]
   public HCPData GetHCPData([FromODataUri] int key)
   {
    ...
   }
}

[My Sample]

Because, at my side, if I implement the controller as follows, it can work:

因为,在我这边,如果我按如下方式实现控制器,它可以工作:

[EnableQuery]
public HCPData GetHCPData([FromODataUri] int key)
{
     var data = new HCPData
     {
         CompanyId = 2,
         Name = "Key = " + key
     };

     return data;
}

Example:

Let me issue the following request:

让我发出以下请求:

如何为表或存储过程中的show result配置Odata api

I can get the following response:

我可以得到以下回复:

{
  "@odata.context":"http://localhost:62591/odata/$metadata#HCPData/$entity","CompanyId":2,"Name":"Key = 11"
}