webapi 基于.net core
布署与调式
-
+
首页
布署与调式
布署到IIS中       另外,如果布署到新机器环境无法运行,则需要.net core 的运行环境 如下几个文件需要安装   布署网站 新建网站,指定物理地址,指定端口 如下图是已经完成的网站  点浏览   该webapi接口 GetData 可以访问自定义的存储过程,和传入多个参数,都是以json方式进行描述,如果switch设置为1的话,在后台的存储过程中的实体为webapi_xxxx但前台参数指定存储过程为xxxx 输入下面的地址可以调试  调试过程 先在数据库中创建存储过程,输入参数自定义如下图所示  在调试接口中输入如下图所示     打开设置的文件如下图   这个配置文件有两种工作方式, 1.如果switch设置为0,则直接读取DataSource,DaName,UserID,Password的配置 这种方式必须要向管理者要取数据库的真实密码后,经工具转换成一个字符串,放在上面的位置上。这种设置不需要OrBitWCF的参与. 2.如果switch设置为1,则读取WCFUser,WCFPassword,WCFAshx的配置。 根据WCFAshx的地址,和用户名,密码来判断是否可以访问WCF。通过后,调取指定的存储过程webapi_xxxxx 并返回数据到前台  上面的openjson如果数据库环境不支持则运行 Alter Database DATABASE Set Compatibility_Level=130 即可,但是SQL2008上面无效 如果switch为1是,而数据库没有上面的存储过程(注意存储过程前面不要加webapi,系统会自动在前面加上)则返回异常信息如下图所示  说明调用了存储过程webapi_demo 没有返回数据到前台 如果填写错误的存储过程则返回  当switch为1时,会 DBSeting的参数,一个都不能少,当switch为0时,下面的几个参数可以给空字符 密码修改后,需要重新启动网站   在postman中的测试为  Json为 { "MethodName":"GetLotInfo", "list":[{ "parametername":"@lotsn", "value":"" } ] } 使用C#调用接口如下   代码如下 private void button1_Click(object sender, EventArgs e) { richTextBox2.Text= HttpPost("http://localhost:8095/OrBitAPI/Getdata", richTextBox1.Text); } public static string HttpPost(string url, string body) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } } 前端ajax调用   网页代码如下 ```html <!<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="jquery-3.4.1.min.js"></script> <script> var obj={"MethodName":"demo","list":[{"parametername":"@lotsn","value":"","parametername":"@moname","value":""}]} $(function () { $("#test").click(function () { //============================= 测试:Test() 方法============================== $.ajax({ type:'post', contentType:'application/json',// 发送数据到服务器时所使用的内容类型。默认是:"application/x-www-form-urlencoded"。 url: "http://localhost:8095/OrbitAPI/getdata",// 规定发送请求的 URL。 dataType:'json',// 预期的服务器响应的数据类型。 //data:JSON.stringify({"MethodName":"GetLotInfo","list":[{"parametername":"@lotsn","value":""}]}), data:JSON.stringify(obj), async: true,// 布尔值,表示请求是否异步处理。默认是 true。 cache: false,// 布尔值,表示浏览器是否缓存被请求页面。默认是 true。 success: function (data) {// 当请求成功时运行的函数。 console.log(data.Table) console.log(data.Table[1]["LotId"]) // 如果走WCF接口返回数据用这个 console.log(data.SQLDataSet) }, error: function (xhr, status, error) {// 如果请求失败要运行的函数。 alert(error+'status'+error.status); alert('失败'); } }); }); $("#test1").click(function () { $.ajax({ type: 'post', url: "http://127.0.0.1:8092/OrbitAPI/test", cache: false, success: function (res) { window.alert(res); }, error: function (err) { window.alert(err.status); } }); }); }); </script> </head> <body> <div style="left:300px;height:30px;width:100px; background-color:yellow; position:absolute;"><input id="test" type="button" value="test" /> <input id="test1" type="button" value="test1" /> </div> </body> </html> ``` 接口文件在天梯培训中的 293 综合设计练习基础篇之这是用于.net core的运行环境安装包 293 综合设计练习基础篇之这是一个webapi的接口 完
ha0002
2022年6月10日 11:08
分享
上一篇
下一篇
目录
微信扫一扫
复制链接
手机扫一扫进行分享
复制链接
下载Markdown文件