Have you ever worship those knock a few lines of code to create the god of the project, learning the commands to create a project you can also become a great God, in fact, very simple command to create the project.
1.cmd command line you intend to create a project location
2. Create the directory solution folder JIYUWU_ONE, then you use the command to create a solution with the following command:
mkdir JIYUWU_ONE cd JIYUWU_ONE dotnet new sln
Note: The name of the default solution is the name of the current directory.
3. Create a Web project, the following command:
dotnet new mvc -o JIYUWU_ONE.Web
As shown below:
4. Create a Common Class Library project, the command is as follows:
dotnet new classlib -o JIYUWU_ONE.Common
As shown below:
5. If you want to JIYUWU_ONE.Common class library project JIYUWU_ONE.Common.csproj added to the solution file, use the following command:
dotnet sln JIYUWU_ONE.sln add JIYUWU_ONE.Common/JIYUWU_ONE.Common.csproj
JIYUWU_ONE.Web project, too, use the following command:
dotnet sln JIYUWU_ONE.sln add JIYUWU_ONE.Web/JIYUWU_ONE.Web.csproj
As shown below:
6 . Add a reference to JIYUWU_ONE.Common.csproj projects in JIYUWU_ONE.Web.csproj project, use the following command:
dotnet add JIYUWU_ONE.Web/JIYUWU_ONE.Web.csproj reference JIYUWU_ONE.Common/JIYUWU_ONE.Common.csproj
如下图所示:
7.在解决方案下每个项目中执行命令:
dotnet restore
如果在某个项目下执行dotnet restore那么则restore的是某个项目,如下图所示:
8.在命令行中执行命令(VS Code安装参考我的上一篇文章):
code .
则使用Visual Studio Code打开该项目,如下图所示:
注意:中间有个空格。
9.我们在Visual Studio Code中找到类库项目,添加一个新类One,并添加一个返回字符串的静态方法
using System; namespace JIYUWU_ONE.Common { public class One { public static string HelloBody() { return "认识你真好,缘来你也想学.Net Core呀!"; } } }
如下图所示:
10.我们在MVC项目的HomeController中,在Privacy方法中添加对类库项目方法的调用
public IActionResult Privacy() { string msg=JIYUWU_ONE.Common.One.HelloBody(); ViewData["Msg"]=msg; return View(); }
如下图所示:
11.修改Privacy.cshtml试图页面
@{ ViewData["Title"] = "Privacy Policy"; } <h1>@ViewData["Title"]</h1> <p>@ViewData["Msg"]</p>
如下图所示:
12.我们回到命令行输入:
dotnet build
编译整个项目,如下图所示:
13.命令行输入:
dotnet publish
发布项目
14.命令行输入:
cd JIYUWU_ONE.Web/bin/Debug/netcoreapp3.0/publish
将目录切换到MVC项目目录,然后在命令行中输入:
dotnet JIYUWU_ONE.Web.dll
来运行项目如图:
15.地址栏输入:https://localhost:5001/Home/Privacy可以看到效果如下图:
16.由于它是不受信任的,我们给它弄个证书吧,执行命令:
dotnet dev-certs https –trust
再次运行看效果
都看到最后了,帮助到你了就动动小手,点个推荐吧!