net core體系-web應用程序-1VS2017構建一個簡單的web

使用vs2017,添加一個新項目-asp.net core web應用程序。css

              

結構如圖,html

        wwwroot放了網站的靜態資源如css、js、image文件;web

        appsetting.json是應用程序的配置文件。json

        buidlerconfig.json是應用程序的打包配置文件。瀏覽器

        page是應用程序的頁面mvc

        program.cs是程序的入口,代碼以下,意思是建立一個站點,並從startup類開始運行。app

 public class Program
{
    public static void Main(string[] args)
    {
           BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().Build();
}
         startup.cs是站點啓動時具體作了哪些事情,主要是開啓了一個mvc服務。asp.net

         打開page網站

         

 

 index 的結構和webform很是類似,在index.cshtml是視圖頁,.cs是後臺程序, 裏面有個onget方法標識被請求是觸發。ui

 

上文提到這個應用程序是啓動了一個mvc服務,那麼咱們能不能把這個webform型搞成mvc的呢,固然能夠。

根目錄下手動建立Controllers文件夾,並建立一個HomeController控制器,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Core.Controllers
{
    public class HomeController : Controller
    {
          // GET: /<controller>/
         public IActionResult Index()
        {
               return View();
        }
    }
}

根目錄下再建立Views文件夾,並添加一個Home文件夾並建立一個Index.cshtml視圖,

@{
        Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
這是mvc的入口頁
</body>
</html>
最後,startup.cs配置路由,找到

app.UseMvc();

追加代碼
app.UseMvc(routes =>
{
      routes.MapRoute(
      name: "default",
      template: "{controller=Home}/{action=Index}/{id?}");
});

運行,在瀏覽器輸入地址http://localhost:49265/Home/Index,運行成功。

相關文章
相關標籤/搜索