项目搭建篇:数据接口 Dear 丶 2021-06-24 16:11 427阅读 0赞 开始之前新建github代码库: [https://github.com/956159241/MyBlogNew.git][https_github.com_956159241_MyBlogNew.git] 数据接口: 新建mvc 4项目,选择web api,更改项目结构,参照: [http://www.jianshu.com/p/ace233099692][http_www.jianshu.com_p_ace233099692] 添加实体数据模型,参照(code first),创建表类,和联系上下文类: [http://www.jianshu.com/p/3858f56e6f0d][http_www.jianshu.com_p_3858f56e6f0d] 文件结构: ![2789632-c7e0f2748d179a74.png][] bll:作为中间处理层 dal:数据处理层 Controler:接口 models:放置返回给前端的实体 entity:放置表类 首先创建博客的主题,和导航,两张表 theme,navigation 支持多个get,post等接口方法: [http://www.jianshu.com/p/15736010bf67][http_www.jianshu.com_p_15736010bf67] 创建一个IndexController,先写两个接口:获取博客主题,获取导航列表,之后再做规范…… 代码: using System; using MyBlogNew.Models; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web; using System.Web.Http; using System.Web.Mvc; using MyBlogNew.Common; using MyBlogNew.Entity; namespace MyBlogNew.Controllers { public class IndexController : ApiController { /// <summary> /// 获取博客主题 /// </summary> /// <returns></returns> public theme GetTheme() { try { using (var entity = new MyBlogNewEntities()) { theme them = entity.themes.FirstOrDefault(); return them; } } catch (Exception ex) { throw (ex); } } /// <summary> /// 获取导航列表 /// </summary> /// <returns></returns> public List<navigation> GetNavigation() { try { using (var entity = new MyBlogNewEntities()) { List<navigation> ls_nav = entity.navigations.ToList(); return ls_nav; } } catch (Exception ex) { throw (ex); } } } } ![2789632-913410115287871f.png][] ![2789632-a273a7c883974209.png][] 发布在iis上面,之后进行前端数据接收的代码编写。 [https_github.com_956159241_MyBlogNew.git]: https://link.jianshu.com?t=https://github.com/956159241/MyBlogNew.git [http_www.jianshu.com_p_ace233099692]: https://www.jianshu.com/p/ace233099692 [http_www.jianshu.com_p_3858f56e6f0d]: https://www.jianshu.com/p/3858f56e6f0d [2789632-c7e0f2748d179a74.png]: /images/20210615/30b8a97a2bca44188d752203643e8f40.png [http_www.jianshu.com_p_15736010bf67]: https://www.jianshu.com/p/15736010bf67 [2789632-913410115287871f.png]: /images/20210615/c593d9a156dd4956ac58a9bfb99f3e7c.png [2789632-a273a7c883974209.png]: /images/20210615/e8c98a49a9624605a647956cd0814d13.png
还没有评论,来说两句吧...