WebAPI写接口


其实真正涉及的代码很少 主要是连接oracle的问题

升级你的EF因为后续用到的CORS需要>6.0版本的EF

CORS 

用于提供跨域服务

在Controller,控制器需要继承ApiController

[EnableCors(origins: "*", headers: "*", methods: "GET,POST")]

在WebAPIConfig

// Web API 配置和服务
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

在Global.asax.cs(去除XML格式)

1 GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

 

ORACLE方面

除此之外还要去oracle官网上下个工具  Oracle Developer Tools for Visual Studio 2017 

http://www.oracle.com/technetwork/topics/dotnet/downloads/odacmsidownloadvs2017-3806459.html

 

在Global.cs中加入(用于输出JSON而非XML)

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

准备工作做完,添加实体模型

Controller部分

    [EnableCors(origins: "*", headers: "*", methods: "GET,POST")]
    public class GeoController : ApiController
    {
        GeoEntities context = new GeoEntities();
        //支持POST、GET请求
        [HttpGet]
        [HttpPost]
        public IEnumerable<ZYML> GetGeoBaseInfo()
        {
            var query = context.ZYML.ToList();
            return query;
        }

        [HttpGet]
        [HttpPost]
        public IEnumerable<ZYML> GetGeoBaseInfoByParam(string queryStr)
        {
            var query = context.ZYML.Where(i => i.XM.Contains(queryStr) || i.ZYMC.Contains(queryStr));
            return query;
        }
    }

最后我还写了个说明文档网页版

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM