0x01、前言
略懂一二。如果有错误,还望见谅。
开发环境:win10+vs2015+sqlserver2014
0x02、demo
新建一个ASP.NET Core Web Application项目--Catcher.EasyDemo.Website
新建三个Class Library项目:
Catcher.EasyDemo.DataAccess:数据访问
Catcher.EasyDemo.Models:模型
Controller项目需要添加MVC的引用:"Microsoft.AspNetCore.Mvc": "1.0.0"
两种方式,一种是和平常一样的右键->添加引用,另一种是在project.json中的dependencies节点下
面添加 "Catcher.EasyDemo.Controllers": "1.0.0-*",然后就会自动restore,完成之后就能正常跑起
下面的话,在Models中添加一个Product类:
namespace Catcher.EasyDemo.Models
{
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductSource { get; set; }
public decimal ProductPrice { get; set; }
}
}
在DataAccess中添加ProductDataAccess类,用于数据交互,里面有用到dapper,所以要添加引用,
以及用到了读取json配置的方法,所以还要添加Microsoft.Extensions.Configuration的引用,同时还要添加Models的引用,方法上面已经说过了。
这里没有用一些复杂的东西,就一个单例模式和一些简单的数据库操作。
using Catcher.EasyDemo.Models;
using Dapper;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq; namespace Catcher.EasyDemo.DataAccess
{
public sealed class ProductDataAccess
{
public static ProductDataAccess Instance
{
get
{
return Nested.instance;
}
} class Nested
{
static Nested() { }
internal static readonly ProductDataAccess instance = new ProductDataAccess();
} /// <summary>
/// get the connection string form the appsettings.json
/// </summary>
/// <returns></returns>
private string GetConnStr()
{
var builder = new ConfigurationBuilder();
builder.SetBasePath(Directory.GetCurrentDirectory());
builder.AddJsonFile("appsettings.json");
var config = builder.Build();
return config.GetConnectionString("dapperConn");
} /// <summary>
/// open the connection
/// </summary>
/// <returns></returns>
private SqlConnection OpenConnection()
{
SqlConnection conn = new SqlConnection(GetConnStr());
conn.Open();
return conn;
} /// <summary>
/// get all products
/// </summary>
/// <returns></returns>
public IList<Product> GetAll()
{
using (IDbConnection conn = OpenConnection())
{
string sql = @"SELECT [ProductId]
,[ProductName]
,[ProductSource]
,[ProductPrice]
FROM [dbo].[Product]";
return conn.Query<Product>(sql).ToList();
}
} /// <summary>
/// delete the product by product's id
/// </summary>
/// <param name="pid">id of the product</param>
/// <returns></returns>
public bool Delete(int pid)
{
using (IDbConnection conn = OpenConnection())
{
string sql = string.Format(@"DELETE FROM [dbo].[Product] WHERE [ProductId]={0} ", pid.ToString());
return conn.Execute(sql) > ;
}
} /// <summary>
/// add the product
/// </summary>
/// <param name="product">entity of the product</param>
/// <returns></returns>
public bool Add(Product product)
{
using (IDbConnection conn = OpenConnection())
{
string sql = string.Format(@"INSERT INTO [dbo].[Product]
([ProductName]
,[ProductSource]
,[ProductPrice])
VALUES
('{0}','{1}',{2})", product.ProductName, product.ProductSource, product.ProductPrice);
return conn.Execute(sql) > ;
}
}
}
}
using Microsoft.AspNetCore.Mvc;
using Catcher.EasyDemo.Models;
using Catcher.EasyDemo.DataAccess; namespace Catcher.EasyDemo.Controllers
{
public class ProductController : Controller
{
/// <summary>
/// Index
/// </summary>
/// <returns></returns>
public IActionResult Index()
{
return View(ProductDataAccess.Instance.GetAll());
} /// <summary>
/// Add
/// </summary>
/// <returns></returns>
public IActionResult Add()
{
return View();
}
[HttpPost]
public IActionResult Add(Product product)
{
bool isOK = ProductDataAccess.Instance.Add(product); if (isOK)
{
return RedirectToAction("Index");
}
else
{
TempData["err"] = "sorry!there were some errors!Please try again.";
return View();
}
} /// <summary>
/// Delete
/// </summary>
/// <param name="pid"></param>
/// <returns></returns>
public IActionResult Delete(int pid)
{
bool isOK = ProductDataAccess.Instance.Delete(pid); if (isOK)
{
return RedirectToAction("Index");
}
else
{
TempData["err"] = "sorry!there were some errors!Please try again.";
return View("Index");
}
}
}
}
添加Index.cshtml
@model IEnumerable<Catcher.EasyDemo.Models.Product>
@{
ViewData["Title"] = "Product Index";
}
<a asp-action="Add" asp-controller="Product">Add a New Product</a>
<div class="container">
<table class="table table-responsive"> <thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Price</td>
<td>Source</td>
<td>Opreation</td>
</tr>
</thead> <tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.ProductId</td>
<td>@item.ProductName</td>
<td>@item.ProductPrice</td>
<td>@item.ProductSource</td>
<td>
<a asp-action="Delete" asp-controller="Product" asp-route-pid="@item.ProductId">Delete</a>
</td>
</tr>
}
</tbody>
</table>
</div>
视图与mvc用的法大致相同,不同就是TagHelper,不过大部分是一看就知道是什么意思,要做什么操作,也不做过多解释。
@model Catcher.EasyDemo.Models.Product
@{
ViewData["Title"] = "Add";
}
<div class="container">
<form asp-action="Add" asp-controller="Product" method="post">
<div class="form-group">
<label asp-for="ProductName">Name</label>
<input asp-for="ProductName" type="text" placeholder="enter the product name" />
</div>
<div class="form-group">
<label asp-for="ProductPrice">Price</label>
<input asp-for="ProductPrice" type="text" placeholder="enter the product price" />
</div>
<div class="form-group">
<label asp-for="ProductSource">Source</label>
<input asp-for="ProductSource" type="text" placeholder="enter the product source" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Product</button>
</div>
</form>
</div>
"connectionStrings": {
"dapperConn": "server=127.0.0.1;database=nancydemo;user id=sa;password=123;"
}
当然,这是在本地的,放到linux时,需要替换成相应的ip
0x03、Linux下运行
在CentOS上安装dotnet core的方式可以看这里,就不在累赘了
安装好了之后,运行dotnet会提示
确定dotnet core 安装成功之后,
进入到相应的目录,运行dotnet 网站对应的dll即可
总之,dotNET Core 用起来感觉不错