开始之前新建github代码库:
https://github.com/956159241/MyBlogNew.git
数据接口:
新建mvc 4项目,选择web api,更改项目结构,参照:
http://www.jianshu.com/p/ace233099692
添加实体数据模型,参照(code first),创建表类,和联系上下文类:
http://www.jianshu.com/p/3858f56e6f0d
文件结构:
![2789632-c7e0f2748d179a74.png](https://i-blog.csdnimg.cn/blog_migrate/c613f816d1bb71f2f7c3411eff68bc03.webp?x-image-process=image/format,png)
bll:作为中间处理层
dal:数据处理层
Controler:接口
models:放置返回给前端的实体
entity:放置表类
首先创建博客的主题,和导航,两张表
theme,navigation
支持多个get,post等接口方法:
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](https://i-blog.csdnimg.cn/blog_migrate/45813803eff6bc34ce018e8053473b44.webp?x-image-process=image/format,png)
![2789632-a273a7c883974209.png](https://i-blog.csdnimg.cn/blog_migrate/595ea76f67a3a58be1d2b0d933b394ab.webp?x-image-process=image/format,png)
发布在iis上面,之后进行前端数据接收的代码编写。