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