0x01、前言

学习ASP.NET Core也有一段时间了,虽说很多内容知识点还是处于一知半解的状态,但是基本的,还是

略懂一二。如果有错误,还望见谅。

本文还是和之前一样,Demo+在Linux下运行(CentOS7+dotnetcore sdk)

开发环境:win10+vs2015+sqlserver2014

0x02、demo

新建一个ASP.NET Core Web Application项目--Catcher.EasyDemo.Website

干掉Controllers文件夹。由于个人习惯问题,习惯性将Controller分离出来。

新建三个Class Library项目:

Catcher.EasyDemo.Controllers:剥离出来的Controller

Catcher.EasyDemo.DataAccess:数据访问

Catcher.EasyDemo.Models:模型

Controller项目需要添加MVC的引用:"Microsoft.AspNetCore.Mvc": "1.0.0"

在Controllers中添加HomeController,内容和生成的是一样的。然后在Website中添加引用,这里有

两种方式,一种是和平常一样的右键->添加引用,另一种是在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) > ;
}
}
}
}
然后在Controllers中添加一个ProductController,具体内容如下:
 
 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");
}
}
}
}
 
控制器的话,应该没有什么太多好说的,毕竟差别不会太大。
 
下面要做的就是添加视图和连接字符串。
 
先添加视图:添加一个Product文件夹,在这里存放相应的视图

添加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,不过大部分是一看就知道是什么意思,要做什么操作,也不做过多解释。

添加Add.cshtml
 
 @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>
 
还要添加的是连接字符串,在appsettings.json中添加一个节点
 
 "connectionStrings": {
"dapperConn": "server=127.0.0.1;database=nancydemo;user id=sa;password=123;"
}

当然,这是在本地的,放到linux时,需要替换成相应的ip

来一张项目截图:
 
来份ASP.NET Core尝尝-LMLPHP

 
到这里,编码工作已经ok了,编译,发布即可

0x03、Linux下运行

这里没有采用jexus的方式部署,原因是想尝尝另外的方式。

在CentOS上安装dotnet core的方式可以看这里,就不在累赘了

安装好了之后,运行dotnet会提示

来份ASP.NET Core尝尝-LMLPHP

确定dotnet core 安装成功之后,

就是把发布后的项目扔到CentOS中,习惯放到/var/www目录下面

进入到相应的目录,运行dotnet 网站对应的dll即可

来份ASP.NET Core尝尝-LMLPHP

并且,在终端还能查看一系列的操作
 
 来份ASP.NET Core尝尝-LMLPHP

总之,dotNET Core 用起来感觉不错

05-07 15:32