PTwo
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # ASP.NET Core 建立 web API - Auto-generated Table of Content [ToC] ###### tags: `.NET Core` ###### 參考資料: > 前往 ➜ [教學課程:使用 ASP.NET Core 建立 web API](https://docs.microsoft.com/zh-tw/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio) > 前往 ➜ [[.NETCore] 安裝 Entity Framework Core](https://marcus116.blogspot.com/2019/04/netcore-how-to-install-entity-framework-core.html) > 前往 ➜ [實作 ASP.NET Core 建立 Web API 教學](https://ithelp.ithome.com.tw/articles/10235984) > 前往 ➜ [Day09 實作官網 ASP.NET Core 教學(一)](https://ithelp.ithome.com.tw/articles/10234296) > 前往 ➜ [.net core 5.0 IIS部屬注意](https://blog.csdn.net/gtosky4u/article/details/113886135) ## :memo: 介紹 ### .NET Core .NET是.NET Framework的新一代版本,是微軟開發的第一個跨平台 (Windows、Mac OSX、Linux)的應用程式開發框架(Application Framework),未來也將會支援FreeBSD與Alpine 平台。.Net Core也是微軟在一開始發展時就開源的軟體平台,它經常也會拿來和現有的開源 .NET 平台Mono比較。 由於 .NET Core 的開發目標是跨平台的 .NET 平台,因此 .NET Core 會包含 .NET Framework 的類別庫,但與 .NET Framework 不同的是 .NET Core 採用套件化 (Packages) 的管理方式,應用程式只需要取得需要的組件即可,與 .NET Framework 大包式安裝的作法截然不同,同時各套件亦有獨立的版本線 (Version line),不再硬性要求應用程式跟隨主線版本。 .NET Core 專案的主要目標有: * 支援或可以移轉 (port) 到更多的作業系統平台與晶片架構 (也就是未來計畫會跨出 x86 平台)。 * 具有引人注目的效能與高可靠度。 * 開發人員能快速與直覺的獲取 .NET Core 開發環境。 * 在直覺與具生產力的情況下建造應用程式,使用檔案,範例與 NuGet 元件。 ## :memo: 建立專案 >Node 版本要求 >Vue CLI需要Node.js 8.9或更高版本(推薦8.11.0+)。 ### Step 1: 使用以下指令進行安裝vue/cli :::info 建立新的專案 ->ASP.NET Core Web 應用程式 -><font color="#EA7500">輸入專案名稱</font> ->目標 Framework = <font color="#EA7500">.NET Core 5.0</font> VS Code 建立 dotnet new webapi -o <font color="#EA7500">專案名稱</font> ::: ### Step 2: NuGET 套件安裝 :::info * Microsoft.EntityFrameworkCore.SqlServer * Microsoft.EntityFrameworkCore.InMemory * Microsoft.VisualStudio.Web.CodeGeneration.Design * Microsoft.EntityFrameworkCore.Design * Swashbuckle.AspNetCore * NetTopologySuite //Geometry型態 * NetTopologySuite.IO.GeoJSON //GeoJSON VS Code 安裝方式 dotnet add package <font color="#EA7500">套件</font> ::: ### Step 3: 導入資料模型 **新增Models資料夾** :::info mkdir Models ::: **導入資料模型指令** :::info Scaffold-DbContext "Server=<font color="#EA7500">domain</font>;Database=<font color="#EA7500">xxxxx</font>;Trusted_Connection=True; User Id=<font color="#EA7500">帳號</font>;Password=<font color="#EA7500">密碼</font>;Integrated Security=false;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models VS Code 指令: dotnet ef dbcontext scaffold "Server=<font color="#EA7500">domain</font>;Database=<font color="#EA7500">xxxxx</font>;Trusted_Connection=True; User Id=<font color="#EA7500">帳號</font>;Password=<font color="#EA7500">密碼</font>;Integrated Security=false;" Microsoft.EntityFrameworkCore.SqlServer -o Models ::: ### Step 4: 連接資料庫 **appsettings.json加入連線資訊** :::info "ConnectionStrings": { "GeoUVP": "Data Source=<font color="#EA7500">domain</font>;Initial Catalog=GeoUVP;User ID=<font color="#EA7500">xxxx</font>;Password=<font color="#EA7500">xxxxx</font>;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" }, ::: **Startup.cs** ::: info using <font color="#EA7500">專案名稱</font>.Models; using Microsoft.EntityFrameworkCore; ``` C# public void ConfigureServices(IServiceCollection services) { services.AddDbContext<Step 3.產生的Context名稱>(opt => opt.UseSqlServer(Configuration.GetConnectionString("appsettings.json的連線資訊名稱->GeoUVP"))); services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "PIFIssuanceAPI", Version = "v1" }); }); services.AddRazorPages(); } ``` 如果使用到空間資料庫,請改成下列方式 ``` C# services.AddDbContext <PIFIssuanceContext> (opt => opt.UseSqlServer(Configuration.GetConnectionString("PIFIssuance"),x => x.UseNetTopologySuite())); ``` ::: :::warning 空間欄位定義方式 Model ``` C# public partial class ParcelIndex { public int QgsFid { get; set; } public Geometry geom { get; set; } public string 地段名 { get; set; } public string 段代碼 { get; set; } public string Maptype { get; set; } public string 製圖法 { get; set; } } ``` Context ```C# modelBuilder.Entity<ParcelIndex>(entity => { entity.HasKey(e => e.QgsFid); entity.ToTable("ParcelIndex"); entity.Property(e => e.geom).HasColumnType("geometry"); entity.Property(e => e.QgsFid).HasColumnName("qgs_fid"); entity.Property(e => e.Maptype).HasColumnName("maptype"); }); ``` ::: ### Step 5: 產生Controllers ** ** :::info 使用 aspnet-codegenerator 產生 controller 程式 名稱: ArticlesController 表名稱: Article Context名稱: ArticleContext dotnet aspnet-codegenerator controller -name <font color="#EA7500">ArticlesController</font> -async -api -m <font color="#EA7500">Article</font> -dc <font color="#EA7500">ArticleContext</font> -outDir Controllers :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully