如何在Asp.Net中将Odata服务转换为Excel [英] How Do I Cinsume Odata Service To Excel In Asp.Net

查看:89
本文介绍了如何在Asp.Net中将Odata服务转换为Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一些这样的代码



vs vs 2013我在Web Api Config中编写代码



i tried some code like this

in vs 2013 i wrote code in "Web Api Config"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.OData.Builder;
using Microsoft.OData.Edm;
using ODataService.Models;
using System.Web.OData.Extensions;
using System.Web.OData;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
//using ODataService.Models;





namespace ODataService
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();
            config.MapODataServiceRoute("odataRoute", "odata", GetModel());

            //config.Services.Replace(typeof(IHttpActionSelector), new System.Web.Http.OData.Routing.ODataActionSelector());
           config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
        public static IEdmModel GetModel()
        {
            DurgaEntities1 obj=new DurgaEntities1();
            var builder = new ODataConventionModelBuilder();
            builder.EntitySet<tbl_Employee>("tbl_Employees");
            ////builder.EntitySet<tbl_Manager>("Mangers");
            //return builder.GetEdmModel();

            var ee = from vv in obj.tbl_Employee.AsEnumerable() select vv;
            return builder.GetEdmModel();







        }
    }
}









i拿了一个控制器并写了一些像这样的代码



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Net ;

使用System.Net.Http;

使用System.Web.Http;

使用System.Web.OData;

使用System.Web.OData.Routing;

使用Odatawithwebapi.Models;



名称空间Odatawithwebapi.Controllers

{

[ODataRoutePrefix(tbl_Employee)]

公共类EmployeeController:ODataController

{

ODataServiceEntities _Entity = new ODataServiceEntities();



[ODataRoute]

[EnableQuery]

public IHttpActionResult GetEmployeesList ()

{

返回Ok(_Enti ty.tbl_Employee);



}

[ODataRoute(({C_Employee_Eno}))]

[EnableQuery]

public IHttpActionResult GetEmployeeusingeno(int _Employeeno)

{



返回Ok(SingleResult.Create< tbl_employee>(_ Entity.tbl_Employee.Where(eno => eno.C_Employee_Eno == _Employeeno)));

}

[ODataRoute]

public IHttpActionResult GetListEmployee(tbl_Employee _EmpList)

{

_Entity.tbl_Employee.Add(_EmpList);

_Entity.SaveChanges();

返回Ok(_EmpList); < br $>


}

[ODataRoute(({C_Employee_Eno}))]



public IHttpActionResult DeleteEmployee(int _Employeeno)

{

var _EmployeeDelete = _Entity.tbl_Employee.Where(eno => eno.C_Employee_Eno == _Employeeno).FirstOrDefault() ;

if(null!= _EmployeeDelete)

{

_Entity.tbl_Employee.Remove(_EmployeeDelete);

}

返回Ok();

}

}

}



但运行/调试我的应用程序我收到错误在WebApiConfig中,像这样









i took one controller and write some code like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Routing;
using Odatawithwebapi.Models;

namespace Odatawithwebapi.Controllers
{
[ODataRoutePrefix("tbl_Employee")]
public class EmployeeController : ODataController
{
ODataServiceEntities _Entity = new ODataServiceEntities();

[ODataRoute]
[EnableQuery]
public IHttpActionResult GetEmployeesList()
{
return Ok(_Entity.tbl_Employee);

}
[ODataRoute("({C_Employee_Eno})")]
[EnableQuery]
public IHttpActionResult GetEmployeeusingeno(int _Employeeno)
{

return Ok(SingleResult.Create<tbl_employee>(_Entity.tbl_Employee.Where(eno => eno.C_Employee_Eno == _Employeeno)));
}
[ODataRoute]
public IHttpActionResult GetListEmployee(tbl_Employee _EmpList)
{
_Entity.tbl_Employee.Add(_EmpList);
_Entity.SaveChanges();
return Ok(_EmpList);

}
[ODataRoute("({C_Employee_Eno})")]

public IHttpActionResult DeleteEmployee(int _Employeeno)
{
var _EmployeeDelete = _Entity.tbl_Employee.Where(eno => eno.C_Employee_Eno == _Employeeno).FirstOrDefault();
if (null != _EmployeeDelete)
{
_Entity.tbl_Employee.Remove(_EmployeeDelete);
}
return Ok();
}
}
}

but run/debug my application i got error in "WebApiConfig" and like this


public static IEdmModel GetModel()
       {
           var builder = new ODataConventionModelBuilder();
           builder.EntitySet<ODataServiceEntities>("tbl_Employee");
         //  builder.EntitySet<ODataServiceEntities>("tbl_Employees");

           return builder.GetEdmModel();
       }





System.Web.OData.dll中出现'System.InvalidOperationException'类型的异常,但未处理在用户代码中



附加信息:实体'ODataServiceEntities'没有定义键。



An exception of type 'System.InvalidOperationException' occurred in System.Web.OData.dll but was not handled in user code

Additional information: The entity 'ODataServiceEntities' does not have a key defined.

推荐答案

这篇关于如何在Asp.Net中将Odata服务转换为Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆