สร้างโปรเจ็กส์ Web Application

  1. สร้างด้วย Visual Studio 2019
  2. สร้างด้วย Visual Studio 2017
  3. สร้างด้วย Command Line
  4. สร้างด้วย Command Line แบบไม่เอา https

1.สร้างด้วย Visual Studio 2019

สร้างโปรเจ็กส์ใหม่ เลือก Create a new project

พิมพ์ ASP.NET เลือกภาษา C#

เลือก ASP.NET Core Web Application template

ตั้งชื่อโปรเจ็กส์ MyCoreApp 

เลือก template เป็น Web Application

และที่ด้านบนเลือกเป็น .NET Core และ ASP.NET Core 2.1

default จะเป็น https ถ้าไม่เอาก็ยกเลิกออก

ลองรันดู จะรันได้ละ

ดูไฟล์ About.cshtml

@page
@model AboutModel
@{
    ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@Model.Message</h3>

<p>Use this area to provide additional information.</p>

ดูไฟล์ About.cshtml.cs

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

namespace MyCoreApp.Pages
{
    public class AboutModel : PageModel
    {
        public string Message { get; set; }

        public void OnGet()
        {
            Message = "Your application description page.";
        }
    }
}

2.สร้างด้วย Visual Studio 2017

สร้างโปรเจ็กส์ใหม่ ตั้งชื่อ WebApp

เลือก template เป็น ASP.NET Core Web Application

เลือก Web Application

เลือกหรือไม่เลือก https (Configure for HTTPS)

รันกด F5

3.สร้างด้วย Command Line

สร้างโปรเจ็กส์แบบ webapp ชื่อโปรเจ็กส์ WebApp1

> dotnet new webapp -o WebApp1
> cd WebApp1

โดย webapp หมายถึงโปรเจ็กส์เว็บแบบ Razor

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}

บรรทัดที่ 22 : เรียกใช้ Startup

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace WebApp1
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc();
        }
    }
}

WebApp1.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
  </ItemGroup>

</Project>

บรรทัดที่ 4 : netcoreapp2.1
บรรทัดที่ 9 : Razor

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

Properties/launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:30887",
      "sslPort": 44379
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApp1": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

บรรทัดที่ 6-7 : กำหนด url และ port ที่ใช้รันจาก Visual Studio
บรรทัดที่ 21 : กำหนด url และ port ที่ใช้รันด้วยคำสั่ง dotnet run

สั่ง run

สั่ง run ด้วย dotnet run

> dotnet run
Using launch settings from C:\Users\jack\source\repos\WebApp1\Properties\launchSettings.json...
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\jack\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Hosting environment: Development
Content root path: C:\Users\jack\source\repos\WebApp1
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

หรือสั่ง run ตัว dll ตรงๆ เลย

> dotnet .\bin\Debug\netcoreapp2.1\WebApp1.dll

เข้าดูเว็บได้ที่ https://localhost:5001
ถ้าจะหยุดรันก็กด Ctrl+C

4.สร้างด้วย Command Line แบบไม่เอา https

สร้างด้วย Command Line แบบมี https (Enable local HTTPS)

> dotnet dev-certs https --trust

สร้างด้วย Command Line แบบไม่เอา https

> dotnet new webapp -o WebApp2 --no-https
> cd .\WebApp2

สั่งรัน

> dotnet run
Using launch settings from C:\Users\jack\source\repos\WebApp2\Properties\launchSettings.json...
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\jack\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Hosting environment: Development
Content root path: C:\Users\jack\source\repos\WebApp2
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

จะเห็นแต่ http ที่พอร์ต 5000