1、新建一個.NetCore控制檯程序web
2、添加依賴項api
3、添加Startup.cs文件瀏覽器
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Text; namespace ConsoleWeb { public class Startup { public void Configure(IApplicationBuilder app) { app.Run(context => { return context.Response.WriteAsync("Hello world"); }); } } }
4、Program.cs文件添加 Microsoft.AspNetCore.Hosting 引用,建立WebHost對象app
using Microsoft.AspNetCore.Hosting; using System; using System.IO; namespace myFirstConsoleDemo { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>() .Build(); host.Run(); } } }
5、運行ui
瀏覽器輸入localhost;5000spa
6、加入WebApicode
項目根目錄下添加一個Api文件夾用來放Api,在Api中新建一個TestApi.cs 繼承自ControllerBase對象
using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Text; namespace ConsoleWeb.Api { public class TestApi:ControllerBase { [Route("test/index")] public string Index() { return "hello api"; } } }
Startup.cs中加入ConfigureServices方法blog
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Text; namespace ConsoleWeb { public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app) { app.UseMvc(s=> { s.MapRoute("default", "{controller}/{action}/{id?}", "test/index"); }); app.Run(context => { return context.Response.WriteAsync("Hello world"); }); } } }
運行程序、在瀏覽器地址欄中輸入localhost:5000/test/index繼承
7、添加頁面,搭建webMVC application
編輯 .csproj文件
到HomeController.cs文件中 選中要添加視圖的Action
添加成功後會自動引入如下幾個NuGet包
如今運行 就能夠看到頁面了