在ASP.NET MVC中使用Web API和EntityFramework构建应用程序
最近做了一个项目技术预研:在ASP.NET MVC框架中使用Web API和EntityFramework,构建一个基础的架构,并在此基础上实现基本的CRUD应用。
以下是详细的步骤。
第一步
在数据库中创建一张数据表,表名为Customer,见下图:
第二步
打开 Visual Studio,新建项目。
选择'ASP.NET Web Application',命名为'WebApi'。
我们要创建一个'Web API',在模板中选择'Web API',点击'确定'。
第三步
接下来我要添加一个类。
右键点击这个 web api项目,添加一个'ADO.NET Entity Data Model'。
添加'EF Designer from database',点击'下一步'。
在配置窗口中添加新连接,输入服务器名称,选择数据库,点击'确定'。
点击'下一步',选择要关联的数据表。
点击'完成'。
第四步
现在的工作就是要为Wep Api添加控制器了。
右键选择'controllers'文件夹。
选择'MVC5 Controller with views, using Entity Framework'。
点击'添加'。
选择数据模型类Customer,选择Data context类文件,本例中为SystemTestEntity。
设置控制器名称为CustomersController,点击'添加'。
上述操作完成后,CustomerController.cs会自动生成。
CustomerController.cs
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Description;
using CRUDWebApiExam.Models;
namespace CRUDWebApiExam.Controllers
{
public class CustomersController : ApiController
{
private SystemTestEntities db = new SystemTestEntities();
// GET: api/Customers
public IQueryable<Customer> GetCustomer()
{
return db.Customer;
}
// GET: api/Customers/5
[ResponseType(typeof(Customer))]
public IHttpActionResult GetCustomer(int id)
{
Customer customer = db.Customer.Find(id);
if (customer == null)
{
return NotFound();
}
return Ok(customer);
}
// PUT: api/Customers/5
[ResponseType(typeof(void))]
public IHttpActionResult PutCustomer(int id, Customer customer)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != customer.CustomerId)
{
return BadRequest();
}
db.Entry(customer).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!CustomerExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Customers
[ResponseType(typeof(Customer))]
public IHttpActionResult PostCustomer(Customer customer)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Customer.Add(customer);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer);
}
// DELETE: api/Customers/5
[ResponseType(typeof(Customer))]
public IHttpActionResult DeleteCustomer(int id)
{
Customer customer = db.Customer.Find(id);
if (customer == null)
{
return NotFound();
}
db.Customer.Remove(customer);
db.SaveChanges();
return Ok(customer);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool CustomerExists(int id)
{
return db.Customer.Count(e => e.CustomerId == id) > 0;
}
}
}
运行这个 Web Api项目,你会看到下面的结果
第五步
到此为止,web api已经构建成功了。接下来我们需要为这个web api服务添加消费对象了,这个消费对象就是本文要涉及的MVC项目。
- 首先要添加一个model类,我们可以直接采用上文中的Customer类。右键点击models文件夹,添加类并命名。本例中,我直接命名为Customer.cs。
Customer.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace MVCPersatantion.Models
{
public class Customer
{
[Display(Name = "CustomerId")]
public int CustomerId { get; set; }
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "Address")]
public string Address { get; set; }
[Display(Name = "MobileNo")]
public string MobileNo { get; set; }
[Display(Name = "Birthdate")]
[DataType(DataType.Date)]
public DateTime Birthdate { get; set; }
[Display(Name = "EmailId")]
public string EmailId { get; set; }
}
}
- customer类添加完成后,接下来需要添加一个ViewModel文件夹,并在里面新建一个CustomerViewModel.cs文件。
CustomerViewModel.cs
using MVCPersatantion.Models;
namespace MVCPersatantion.ViewModel
{
public class CustomerViewModel
{
public Customer customer { get; set; }
}
}
第六步
我已经添加了一个消费web api服务的类,现在我还需要添加一个Client类。
- 右键点击models文件夹,添加类文件CustomerClient.cs。
CustomerClient.cs
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
namespace MVCPersatantion.Models
{
public class CustomerClient
{
private string Base_URL = "http://localhost:30110/api/";
public IEnumerable<Customer> findAll()
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Base_URL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("customers").Result;
if (response.IsSuccessStatusCode)
return response.Content.ReadAsAsync<IEnumerable<Customer>>().Result;
return null;
}
catch
{
return null;
}
}
public Customer find(int id)
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Base_URL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("customers/" + id).Result;
if (response.IsSuccessStatusCode)
return response.Content.ReadAsAsync<Customer>().Result;
return null;
}
catch
{
return null;
}
}
public bool Create(Customer customer)
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Base_URL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsJsonAsync("customers", customer).Result;
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
public bool Edit(Customer customer)
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Base_URL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PutAsJsonAsync("customers/" + customer.CustomerId, customer).Result;
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
public bool Delete(int id)
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Base_URL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.DeleteAsync("customers/" + id).Result;
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
}
}
细心的朋友们可能已经发现,我的代码中有一些重复代码。实际应用中我们可以抽取成公用代码,这样任何地方都能够使用了。这里为了清楚的阐述过程,所以写了一些重复的代码。
我们看第一行private string Base_URL = "http://localhost:50985/api/"
,这个Base_URL是web api的服务地址。
接下来就可以编写Insert、Update、Delete、Select的业务逻辑了。
第七步
右键点击Controllers文件夹,添加一个名为Customer控制器,控制器中添加服务客户端调用的方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCPersatantion.Models;
using MVCPersatantion.ViewModel;
namespace MVCPersatantion.Controllers
{
public class CustomerController : Controller
{
// GET: Customer
public ActionResult Index()
{
CustomerClient CC = new CustomerClient();
ViewBag.listCustomers = CC.findAll();
return View();
}
[HttpGet]
public ActionResult Create()
{
return View("Create");
}
[HttpPost]
public ActionResult Create(CustomerViewModel cvm)
{
CustomerClient CC = new CustomerClient();
CC.Create(cvm.customer);
return RedirectToAction("Index");
}
public ActionResult Delete(int id)
{
CustomerClient CC = new CustomerClient();
CC.Delete(id);
return RedirectToAction("Index");
}
[HttpGet]
public ActionResult Edit(int id)
{
CustomerClient CC = new CustomerClient();
CustomerViewModel CVM = new CustomerViewModel();
CVM.customer = CC.find(id);
return View("Edit",CVM);
}
[HttpPost]
public ActionResult Edit(CustomerViewModel CVM)
{
CustomerClient CC = new CustomerClient();
CC.Edit(CVM.customer);
return RedirectToAction("Index");
}
}
}
第八步
创建视图页面Index.cshtml。
在Index页面中添加代码。
Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div align="center">
@Html.ActionLink("Create", "Create", "Customer", new { area = "" }, null)
<br /><br />
<table cellpadding="2"
class="table"
cellspacing="2"
border="1">
<tr class="btn-primary">
<th>
CustomerId
</th>
<th> Name</th>
<th> Address</th>
<th> MobileNo</th>
<th> Birthdate</th>
<th> EmailId</th>
<th> Action</th>
</tr>
@foreach (var Cust in ViewBag.listCustomers)
{
<tr class="btn-success">
<td>
@Cust.CustomerId
</td>
<td>
@Cust.Name
</td>
<td>
@Cust.Address
</td>
<td>
@Cust.MobileNo
</td>
<td>
@Cust.Birthdate.ToString("dd/MM/yyyy")
</td>
<td>
@Cust.EmailId
</td>
<td>
<a onclick="return confirm('确定要删除这条数据?')"
href="@Url.Action("Delete","Customer",new {id= Cust.CustomerId})">Delete</a> ||
<a href="@Url.Action("Edit","Customer",new {id= Cust.CustomerId})">Edit</a>
@*@Html.ActionLink("Edit", "Edit", "Customer", new { id = Cust.CustomerId })*@
</td>
</tr>
}
</table>
</div>
添加新增数据模块Create。
在Customer控制器中添加create方法,在view中增加Create.cshtml。
Create.cshtml
@model WebApiMVC.ViewModel.CustomerViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<div align = "center" >
@using (Html.BeginForm("create", "Customer", FormMethod.Post))
{
<table class="table">
<tr>
<td>
@Html.LabelFor(model => model.customer.Name)
</td>
<td>
@Html.TextBoxFor(model => model.customer.Name)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.customer.Address)
</td>
<td>
@Html.TextBoxFor(model => model.customer.Address)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.customer.MobileNo)
</td>
<td>
@Html.TextBoxFor(model => model.customer.MobileNo)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.customer.Birthdate)
</td>
<td>
@Html.TextBoxFor(model => model.customer.Birthdate)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.customer.EmailId)
</td>
<td>
@Html.TextBoxFor(model => model.customer.EmailId)
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="保存" />
</td>
</tr>
</table>
}
</div>
添加修改数据模块Edit。
在Customer控制器中添加edit方法,在view中增加Edit.cshtml。
Edit.cshtml
@{
ViewBag.Title = "Edit";
}
@model WebApiMVC.ViewModel.CustomerViewModel
<h2> Edit </h2>
<div align="center" width="500px">
@using (Html.BeginForm("Edit", "Customer", FormMethod.Post))
{
<table class="table"
width="400px"
cellpadding="20">
<tr class="btn-default">
<td>
@Html.LabelFor(model => model.customer.Name)
</td>
<td>
@Html.TextBoxFor(model => model.customer.Name)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.customer.Address)
</td>
<td>
@Html.TextBoxFor(model => model.customer.Address)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.customer.MobileNo)
</td>
<td>
@Html.TextBoxFor(model => model.customer.MobileNo)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.customer.Birthdate)
</td>
<td>
@Html.TextBoxFor(model => model.customer.Birthdate)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.customer.EmailId)
</td>
<td>
@Html.TextBoxFor(model => model.customer.EmailId)
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" value="保存" /> <a class="btn-primary" href="@Url.Action("Index ","Customer ")"> 返回 </a>
@Html.HiddenFor(model => model.customer.CustomerId)
</td>
</tr>
</table>
}
</div>
最终项目代码已经完成了。这时候我们运行项目试试看了。但还有一点要注意的是,我们必须同时运行web api和mvc项目。怎么办呢?其实只要在项目属性里设置就可以了。
- 右键解决方案,选择属性(Properties)。
接来下在启动项目选项中,选择
多项目启动
,在Action
中设置两个项目同时启动。运行项目,看看CRUD操作的执行结果。
Select
Insert
Update
Delete
这是整个技术预研的过程,本文末尾位置提供代码下载。
博客地址:http://www.cnblogs.com/yayazi/
本文地址:http://www.cnblogs.com/yayazi/p/8444054.html
声明:本博客原创文字允许转载,转载时必须保留此段声明,且在文章页面明显位置给出原文链接。
源码下载:https://github.com/daivven/WebApiMVC
PS:本人近期打算换工作,目标地点武汉,目标职位.Net软件工程师,期望月薪资12k以上,春节后可参加面试。有没有园友有合适岗位推荐,私信我可发简历,求内推哇~
在ASP.NET MVC中使用Web API和EntityFramework构建应用程序的更多相关文章
- MVC中使用Web API和EntityFramework
在ASP.NET MVC中使用Web API和EntityFramework构建应用程序 最近做了一个项目技术预研:在ASP.NET MVC框架中使用Web API和EntityFramework ...
- [译]ABP框架使用AngularJs,ASP.NET MVC,Web API和EntityFramework构建N层架构的SPA应用程序
本文转自:http://www.skcode.cn/archives/281 本文演示ABP框架如何使用AngularJs,ASP.NET MVC,Web API 和EntityFramework构建 ...
- 如何将一个 ASP.NET MVC 4 和 Web API 项目升级到 ASP.NET MVC 5 和 Web API 2
----转自微软官网www.asp.net/mvc/ ASP.NET MVC 5 和 Web API 2 带来的新功能,包括属性路由. 身份验证筛选器,以及更多的主机.请参阅http://www.as ...
- ASP.NET MVC4中调用WEB API的四个方法
http://tech.it168.com/a2012/0606/1357/000001357231_all.shtml [IT168技术]当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各 ...
- ASP.NET MVC View 和 Web API 的基本权限验证
ASP.NET MVC 5.0已经发布一段时间了,适应了一段时间,准备把原来的MVC项目重构了一遍,先把基本权限验证这块记录一下. 环境:Windows 7 Professional SP1 + Mi ...
- ASP.NET MVC中加入Web Forms
目的 有时候在一个ASP.NET MVC项目发布之后,又需要添加动态页面,同时又不想重新在源代码中添加view,那么这时候就要用上Web Forms了. 步骤 1.在项目根目录添加一个文件夹,在文件夹 ...
- 【ASP.NET Core学习】Web API
这里介绍在ASP.NET Core中使用Web API创建 RESTful 服务,本文使用VSCode + NET Core3.0 创建简单Rest API 格式化输出 JSON Patch请求 Op ...
- ASP.NET MVC中加载WebForms用户控件(.ascx)
原文:ASP.NET MVC中加载WebForms用户控件(.ascx) 问题背景 博客园博客中的日历用的是ASP.NET WebForms的日历控件(System.Web.UI.WebControl ...
- ASP.NET MVC + 百度富文本编辑器 + EasyUi + EntityFrameWork 制作一个添加新闻功能
本文将交大伙怎么集成ASP.NET MVC + 百度富文本编辑器 + EasyUi + EntityFrameWork来制作一个新闻系统 先上截图: 添加页面如下: 下面来看代码部分 列表页如下: @ ...
随机推荐
- os系统
任务延时函数OSTimeDly 功能:调用该函数的任务将自己延时一段时间并执行一次任务调度,一旦规定的延时时间完成或有其它的任务通过调用OSTimeDlyResume()取消了延时,调用OSTimeD ...
- c++(查找)
无论是数据库,还是普通的ERP系统,查找功能数据处理的一个基本功能.数据查找并不复杂,但是如何实现数据又快又好地查找呢?前人在实践中积累的一些方法,值得我们好好学些一下.我们假定查找的数据唯一存在,数 ...
- 独立成分分析(ICA)的模拟实验(R语言)
本笔记是ESL14.7节图14.42的模拟过程.第一部分将以ProDenICA法为例试图介绍ICA的整个计算过程:第二部分将比较ProDenICA.FastICA以及KernelICA这种方法,试图重 ...
- [国嵌笔记][033-034][设置svc模式]
[设置svc模式] 设置CPU为SVC模式 1.因为初始化系统需要有很高的权限,SVC模式具有该权限,所以首先要使系统工作在SVC(0b10011)模式 2.设置cprs为0xd3(0b1101001 ...
- HDU 5538 House Building(模拟——思维)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5538 Problem Description Have you ever played the vi ...
- sha1() 函数
sha1() 函数计算字符串的 SHA-1 散列. sha1() 函数使用美国 Secure Hash 算法 1. 来自 RFC 3174 的解释 - 美国 Secure Hash 算法 1:SHA- ...
- two Pass方法连通域检测
原理: Two-Pass方法检测连通域的原理可参见这篇博客:http://blog.csdn.net/lichengyu/article/details/13986521. 参考下面动图,一目了然. ...
- JavaScript对象的valueOf()方法
js对象中的valueOf()方法和toString()方法非常类似,但是,当需要返回对象的原始值而非字符串的时候才调用它,尤其是转换为数字的时候.如果在需要使用原始值的上下文中使用了对象,JavaS ...
- 一个HTTP Basic Authentication引发的异常
这几天在做一个功能,其实很简单.就是调用几个外部的API,返回数据后进行组装然后成为新的接口.其中一个API是一个很奇葩的API,虽然是基于HTTP的,但既没有基于SOAP规范,也不是Restful风 ...
- JSP -java service pages
jsp的运行机制: 1客户端(请求jsp页面)------->JSP页面(转换为java源文件)------->Selvet文件(.java) (编译成相应的字节码文件也就是后缀名是.cl ...