当前位置: 首页 > news >正文

做网站的行情哪里可以申请免费域名

做网站的行情,哪里可以申请免费域名,教育行业手机wap网站,免费公网服务器一.创建web api项目 1.1、项目创建 MVC架构的话#xff0c;它会有view-model-control三层#xff0c;在web api中它的前端和后端是分离的#xff0c;所以只在项目中存在model-control两层 1.2、修改路由 打开App_Start文件夹下#xff0c;WebApiConfig.cs ,修改路由… 一.创建web api项目 1.1、项目创建 MVC架构的话它会有view-model-control三层在web api中它的前端和后端是分离的所以只在项目中存在model-control两层 1.2、修改路由 打开App_Start文件夹下WebApiConfig.cs ,修改路由加上{action}/ 这样就可以在api接口中通过接口函数名来导向我们希望调用的api函数否则只能通过controller来导向就可能会造成有相同参数的不同名函数冲突。其中{id}是api接口函数中的参数。   默认路由配置信息为【默认路由模板无法满足针对一种资源一种请求方式的多种操作。】 WebApi的默认路由是通过http的方法get/post/put/delete去匹配对应的action也就是说webapi的默认路由并不需要指定action的名称 using System; using System.Collections.Generic; using System.Linq; using System.Web.Http;namespace WebAPI {public static class WebApiConfig{public static void Register(HttpConfiguration config){// Web API 配置和服务// Web API 路由config.MapHttpAttributeRoutes();config.Routes.MapHttpRoute(name: DefaultApi,//修改路由加上{action}/ 这样就可以在api接口中通过接口函数名来导向我们希望调用的api函数//否则只能通过controller来导向就可能会造成有相同参数的不同名函数冲突。其中{id}是api接口函数中的参数routeTemplate: api/{controller}/{action}/{id},defaults: new { id RouteParameter.Optional });}} }二.测试案例 写一个测试的api函数并开始执行(不调试) 2.1、我们在model文件夹中添加一个类movie using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace WebAPI.Models {public class movie{public string name { get; set; }public string director { get; set; }public string actor { get; set; }public string type { get; set; }public int price { get; set; }} } 2.1.2、我们在model文件夹中添加一个类Product using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace WebAPI.Models {public class Product{public int Id { get; set; }public string Name { get; set; }public string Category { get; set; }public decimal Price { get; set; }} } 2.2、在controller文件夹下添加web api控制器命名改为TestController using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using WebAPI.Models;namespace WebAPI.Controllers {public class TestController : ApiController{movie[] mymovie new movie[]{new movie { name海蒂和爷爷,director阿兰.葛斯彭纳,actor阿努克,type动漫,price28},new movie { name云南虫谷,director佚名,actor潘粤明,type惊悚,price32},new movie { name沙海,director佚名,actor吴磊,type惊悚,price28},new movie { name千与千寻,director宫崎骏,actor千寻,type动漫,price28}};public IEnumerablemovie GetAllMovies(){return mymovie;}public IHttpActionResult GetMovie(string name) //异步方式创建有什么作用{var mov mymovie.FirstOrDefault((p) p.name name);if (mymovie null){return NotFound();}return Ok(mymovie);}} }这样就完成了一个web api实例的编写 2.2.2、在controller文件夹下添加web api控制器命名改为productsController using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using WebAPI.Models;namespace WebAPI.Controllers {public class productsController : ApiController{Product[] products new Product[]{new Product { Id 1, Name Tomato Soup, Category Groceries, Price 1 },new Product { Id 2, Name Yo-yo, Category Toys, Price 3.75M },new Product { Id 3, Name Hammer, Category Hardware, Price 16.99M }};public IEnumerableProduct GetAllProducts(){return products;}public IHttpActionResult GetProduct(int id){var product products.FirstOrDefault((p) p.Id id);if (product null){return NotFound();}return Ok(product);}} }2.2.3、在controller文件夹下添加web api控制器命名改为MyController using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http;namespace WebAPI.Controllers {public class MyController : ApiController{[HttpGet]public string MyExample(string param1, int param2){string res ;res param1 param2.ToString();//这边可以进行任意操作比如数据存入或者取出数据库等return res;}} }三.本地调试 3.1 运行调试以本地 localhost或127.0.0.1形式访问 ①点击工具栏【IIS Express】 ②浏览地址输入接口看是否可以访问 localhost:44381/api/products/GetAllProducts 注意 这里的路径是写你的控制器前缀名称Control文件下的productsController控制器文件的前缀 https://localhost:44381/api/Test/GetAllMovies 2直接在浏览器中调试也行 想要调试的值可以将WebApiConfig.cs的代码修如下 using System; using System.Collections.Generic; using System.Linq; using System.Web.Http;namespace WebAPI {public static class WebApiConfig{public static void Register(HttpConfiguration config){// Web API 配置和服务// Web API 路由config.MapHttpAttributeRoutes();config.Routes.MapHttpRoute(name: DefaultApi,//修改路由加上{action}/ 这样就可以在api接口中通过接口函数名来导向我们希望调用的api函数//否则只能通过controller来导向就可能会造成有相同参数的不同名函数冲突。其中{id}是api接口函数中的参数routeTemplate: api/{controller}/{action}/{id},defaults: new { id RouteParameter.Optional });//去掉xml返回格式、设置json字段命名采用var appXmlType config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t t.MediaType application/xml);config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);}} }ok显示成功 localhost:44381/api/My/MyExample?param1param22 WebApi项目实例3-1 3-1 (1)新添加到控制器UserInfoController, using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using WebAPI.Models;namespace WebAPI.Controllers {public class UserInfoController : ApiController{//检查用户名是否已注册private ApiTools tool new ApiTools();// [HttpPost][HttpGet]public HttpResponseMessage CheckUserName(string _userName){int num UserInfoGetCount(_userName);//查询是否存在该用户if (num 0){return tool.MsgFormat(ResponseCode.操作失败, 不可注册/用户已注册, 1 _userName);}else{return tool.MsgFormat(ResponseCode.成功, 可注册, 0 _userName);}}private int UserInfoGetCount(string username){//return Convert.ToInt32(SearchValue(select count(id) from userinfo where username username ));return username admin ? 1 : 0;}} }添加返回响应类ApiTools using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text.RegularExpressions; using System.Web;namespace WebAPI.Models {//添加返回响应类public class ApiTools{private string msgModel {{\code\:{0},\message\:\{1}\,\result\:{2}}};public ApiTools(){}public HttpResponseMessage MsgFormat(ResponseCode code, string explanation, string result){string r ^(\-|\)?\d(\.\d)?$;string json string.Empty;if (Regex.IsMatch(result, r) || result.ToLower() true || result.ToLower() false || result [] || result.Contains({)){json string.Format(msgModel, (int)code, explanation, result);}else{if (result.Contains()){json string.Format(msgModel, (int)code, explanation, result);}else{json string.Format(msgModel, (int)code, explanation, \ result \);}}return new HttpResponseMessage { Content new StringContent(json, System.Text.Encoding.UTF8, application/json) };}}public enum ResponseCode{操作失败 00000,成功 10200,}} 3-1 (2)本地调试调用Web API接口 运行调试以本地 localhost或127.0.0.1形式访问 ①点击工具栏【IIS Express】 ②浏览地址输入接口看是否可以访问 https://localhost:44381/api/UserInfo/CheckUserName?_userNamewxd 3.2 运行调试以本地IP192.168.6.152形式访问 127.0.0.1是回路地址来检验本机TCP/IP协议栈实际使用过程中服务端不在本机是外部地址要用IP地址测试。 外部用户采用IP端口号访问如下图浏览器访问不了400错误。   解决方案 因为 IIS 7 采用了更安全的 web.config 管理机制默认情况下会锁住配置项不允许更改。 以管理员身份运行命令行【此处不要操作】 C:\windows\system32\inetsrv\appcmd unlock config -section:system.webServer/handlers 如果modules也被锁定再运行 C:\windows\system32\inetsrv\appcmd unlock config -section:system.webServer/modules 客户端程序调用接口分为以下几种情况 通过Javascript 和 jQuery 调用 Web API 右键资源管理器解决方案下面的项目添加-新建项 将index.html内容替换成 !DOCTYPE html html xmlnshttp://www.w3.org/1999/xhtml headtitleProduct App/title /head bodydivh2All Products/h2ul idproducts //divdivh2Search by ID/h2input typetext idprodId size5 /input typebutton valueSearch onclickfind(); /p idproduct //divscript srchttps://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js/scriptscriptvar uri api/Products;$(document).ready(function () {// Send an AJAX request$.getJSON(uri).done(function (data) {// On success, data contains a list of products.$.each(data, function (key, item) {// Add a list item for the product.$(li, { text: formatItem(item) }).appendTo($(#products));});});});function formatItem(item) {return item.Name : $ item.Price;}function find() {var id $(#prodId).val();$.getJSON(uri / id).done(function (data) {$(#product).text(formatItem(data));}).fail(function (jqXHR, textStatus, err) {$(#product).text(Error: err);});}/script /body /html四.发布web api 并部署 4.1、首先右键项目选择发布 到这里程序已经发布到指定的路径下了这里的路径可以是本机的文件夹也可以是服务器上的ftp路径 4.2、我们还剩最后一步就是在IIS上把发布的服务端程序挂上去不说了直接上图 打开iis选中网站右键 添加网站  好了服务端程序发布并部署完成。 这个WebAPI就是刚刚我们部署好的点击下图右侧的浏览*91(http)会打开网页
http://www.hkea.cn/news/14267172/

相关文章:

  • 做网站,用什么做数据库最好2345网址导航应用
  • 贵州省建设厅官网站企业网厅
  • 织梦网站主页底企业服务平台公众号
  • 网站按钮确定后图片怎么做去哪个网站有客户找做标书的
  • wordpress网站关闭平面设计培训多少钱 贵吗
  • 海宁网站建设公司推荐在线A视频网站l一级A做爰片
  • 建设网站的费用网站的建设分析
  • 行政单位网站建设立项依据看别人的wordpress
  • 学网站开发难吗移动版wordpress主题
  • 网站页面怎么做wordpress发表的文章在页面找不到
  • 元谋县建设局网站建设网站要学编程吗
  • 网站建设与运营推广的回报材料滨海做网站哪家最好
  • 做不锈钢的网站有哪些做网站需要多少固定带宽
  • 如何阿里网站建设刷外链
  • 杭州网站的制作北京东城网站建设公司
  • 赣州网站建设开发南宁制作网站服务商
  • wordpress新建数据库wordpress做seo好做
  • 东海县做网站广告网站建设系统服务
  • 网站版权文字网页前端设计包括哪些内容
  • 网站推广策划思路是什么网站设计要点 优帮云
  • 网站接电话上海做公司网站多少钱
  • 湖南网站备案要多少天wordpress 720云
  • 如何选择盐城网站开发网站如何兼容ie6
  • 南京家具网站建设人力资源外包平台
  • 惠州网站制作软件商务网站建设包含了
  • 秦皇岛网站制作源码红色基调的网站
  • 网站开发+演讲西安公司注销
  • 网站推广排名优化多少钱聊石家庄seo
  • 有没有做网站源代码修改的做的网站浏览的越多越挣钱
  • 东莞商城网站建设公司哈尔滨网页设计与制作