demo+ running under Linux (Centos7+dotnetcore SDK)

Source: Internet
Author: User
Tags button type dotnet

Take a sample of the net core to taste 0x01, the preface to learn ASP. NET core also has a period of time, although a lot of content knowledge points are still in the state of smattering, but the basic, or

Slightly understand one or two. If there is an error, I hope you forgive me.

This article is still the same as before, demo+ runs under Linux (Centos7+dotnetcore SDK)

Development environment: WIN10+VS2015+SQLSERVER2014

0x02, Demo

Create a new ASP. NET Core Web Application project--catcher.easydemo.website

Kill the Controllers folder. Because of personal habits, the controller is habitually separated.

Create a new three Class library project:

Catcher.EasyDemo.Controllers: Stripped-out controller

Catcher.EasyDemo.DataAccess: Data access

Catcher.EasyDemo.Models: Model

The controller project needs to add a reference to MVC: "MICROSOFT.ASPNETCORE.MVC": "1.0.0"

Add HomeController in controllers and the content is the same as the build. Then add the reference in the website, here are

Two ways, one is the same as the common right-click Add Reference, the other is in the Project.json in the Dependencies node

Add "Catcher.EasyDemo.Controllers": "1.0.0-*", then automatically restore, after the completion of the normal run

Here it comes. (Not here)

In the following case, add a product class in models:

1 namespace Catcher.EasyDemo.Models 2 {3 Public     class Product 4     {5 public         int ProductId {get; set;} 6         public string ProductName {get, set;} 7 public         string Productsource {get, set;} 8 public         decimal Productprice {get; set;} 9     }10}

Add the Productdataaccess class in dataaccess for data interaction, which is useful to dapper, so to add a reference,

And the use of the method of reading the JSON configuration, so also add the Microsoft.Extensions.Configuration reference, but also add the models reference, the method has been said above.

There are no complicated things to do here, just a singleton pattern and some simple database operations.

  1 using Catcher.EasyDemo.Models;  2 using Dapper;  3 using Microsoft.Extensions.Configuration;  4 using System.Collections.Generic;  5 using System.Data;  6 using System.Data.SqlClient;  7 using System.IO;  8 using System.Linq; 9 namespace Catcher.EasyDemo.DataAccess Each {sealed class productdataaccess Tatic productdataaccess Instance {+ get + + Nested.instanc E (+}), class Nested, {Nested () {} nternal static readonly Productdataaccess instance = new Productdataaccess ();         +/-<summary>//Get the connection string form the Appsettings.json 30         </summary>//<returns></returns> + private String getconnstr () 33 {The var builder = new Configurationbuilder (); BuilDer. Setbasepath (Directory.GetCurrentDirectory ()); * Builder. Addjsonfile ("Appsettings.json"); Notoginseng var config = builder.              Build (); return CONFIG. GetConnectionString ("Dapperconn"); */<summary>//Open the connection//</summ             ary>//<returns></returns> SqlConnection OpenConnection () 46 {47 SqlConnection conn = new SqlConnection (GETCONNSTR ()); Conn. Open (); Conn return;         *//<summary>//Get all products///</summary> 55 <returns></returns> public ilist<product> GetAll ()                                   Bconnection conn = OpenConnection ()) {$ sql = @ "SELECT [ProductId] 61  , [ProductName] 62                                 , [Productsource], [Productprice] 64 from [dbo]. [Product] "; Conn return. query<product> (SQL). ToList ();         *//<summary>//delete the product by product ' s ID 71 </summary>//<param name= "pid" >id of the product</param>///<returns ></returns> public bool Delete (int pid) (IDbConnection Co nn = OpenConnection ()). Format (@ "DELETE from [dbo].[ Product] WHERE [productid]={0} ", PID. ToString ()); Conn return. Execute (SQL) > 0; +/-<summary>//Add the product//</summar y>//<param name= "Product" >entity of the product</param>//<returns></returns>-public bool Add (product product) 89 {90 The using (IDbConnection conn = OpenConnection ()) is a string sql = string. Format (@ "INSERT into [dbo].[            Product] ([ProductName] 94, [Productsource], [Productprice]) 97 (' {0} ', ' {1} ', {2}) ', product. ProductName, product. Productsource, product. Productprice); 98 Return Conn. Execute (SQL) > 0; 99}100}101}102}
Then add a productcontroller in the controllers, as follows:
 1 using MICROSOFT.ASPNETCORE.MVC; 2 using Catcher.EasyDemo.Models; 3 using Catcher.EasyDemo.DataAccess; 4 5 Namespace Catcher.EasyDemo.Controllers 6 {7 public class Productcontroller:controller 8 {9///         LT;SUMMARY&GT;10//Index11//</summary>12//<returns></returns> 13 Public Iactionresult Index () {return View (ProductDataAccess.Instance.GetAll ()) :}17//<summary>19//ADD20//</summary>21//<returns& Gt;</returns>22 public Iactionresult Add () () (),}26 [H ttppost]27 Public Iactionresult ADD (product product), {$ bool IsOK = Productdataaccess.insta nCE.             ADD (product), if (IsOK) + ("Index"), 34 }35 Else36 {PNS tempdata["err"] = "Sorry!there were some errors! Please try again. ";         }40}41//<summary>43//Delete44 &LT;/SUMMARY&GT;45//<param name= "pid" ></param>46//<returns></returns&gt ; public iactionresult Delete (int pid)-{IsOK = ProductDataAccess.Instance.Delete (P             (IsOK), ("Index"), and 54}55 else56 {tempdata["err"] = "Sorry!there were some errors! Please try again. "; ("Index"), 59}60}61}62}
Controller, there should be no too much to say, after all, the difference will not be too big. The next thing to do is add the view and the connection string. Add a view first: Add a product folder where the corresponding view is stored

Add index.cshtml

 1 @model ienumerable<catcher.easydemo.models.product> 2 @{3 viewdata["Title"] = "Product Index"; 4} 5 <a A sp-action= "Add" asp-controller= "Product" >add a New product</a> 6 <div class= "container" > 7 <table CL ass= "Table Table-responsive" > 8 9 <thead>10 <tr>11 <td>id</td >12 <td>name</td>13 <td>price</td>14 <td>          source</td>15 <td>opreation</td>16 </tr>17 </thead>18 19                     <tbody>20 @foreach (var item in Model) {<tr>23 <td> @item. Productid</td>24 <td> @item. Productname</td>25 <td> @item. Productprice</td>26 <td> @item. Productsource</td>27 <td>28                         <a asp-action= "Delete" asp-controller= "Product" asp-route-pid= "@item. ProductId ">delete</a>29 </td>30 </tr>31}32 & Lt;/tbody>33 </table>34 </div>

Views are roughly the same as those used by MVC, except that they are taghelper, but most of them know what they mean, what they do, and don't explain too much.

Add add.cshtml
 1 @model Catcher.EasyDemo.Models.Product 2 @{3 viewdata["Title"] = "ADD"; 4} 5 <div class= "Container" > 6             <form asp-action= "Add" asp-controller= "Product" method= "POST" > 7 <div class= "Form-group" > 8 <label asp-for= "ProductName" >Name</label> 9 <input asp-for= "ProductName" type= "text" Placehol  Der= "Enter the product name"/>10 </div>11 <div class= "Form-group" >12 <label Asp-for= "Productprice" >price</label>13 <input asp-for= "Productprice" type= "text" placeholder= "ent Er the product price "/>14 </div>15 <div class=" Form-group ">16 <label asp-fo R= "Productsource" >source</label>17 <input asp-for= "Productsource" type= "text" placeholder= "Enter t He product source "/>18 </div>19 <div class=" Form-group ">20 <button type=" su Bmit "class=" btn Btn-priMary ">add product</button>21 </div>22 </form>23 </div> 
Also add a connection string to add a node to the Appsettings.json
1 "connectionStrings": {2     "dapperconn": "Server=127.0.0.1;database=nancydemo;user id=sa;password=123;" 3   }

Of course, this is local, and when placed on Linux, it needs to be replaced with the corresponding IP

Come to a project: here, the coding work is OK, compile, release can 0x03, Linux run here is not a jexus way to deploy, because you want to try another way.

The way to install dotnet core on CentOS can be seen here, it's not a liability.

Https://www.microsoft.com/net/core#centos

Once installed, running dotnet will prompt

After you have determined that the dotnet core installation was successful,

is to throw the post-project into CentOS, and get used to the/var/www directory.

Go to the appropriate directory and run the DLL for the dotnet Web site.

Also, the terminal can view a series of operations

In short, DotNET Core feels good to use.

demo+ running under Linux (Centos7+dotnetcore SDK)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.