zoukankan      html  css  js  c++  java
  • .netcore入门

    开发环境:windows    编辑器: Visual Studio Code

    环境安装:

    .Net Core 1.1 SDK     https://www.microsoft.com/net/core#windowscmd

    1.新建控制台项目(参考:https://docs.microsoft.com/en-us/dotnet/articles/core/tutorials/using-with-xplat-cli)

    1.1 安装.Net Core 1.1 SDK完成后,打开cmd命令窗口,输入dotnet,可以看到如下,说明安装成功

    E:lindy
    etcoreprojectaspnetcoreapp>dotnet
    
    Microsoft .NET Core Shared Framework Host
    
      Version  : 1.1.0
      Build    : 928f77c4bc3f49d892459992fb6e1d5542cb5e86
    
    Usage: dotnet [common-options] [[options] path-to-application]
    
    Common Options:
      --help                           Display .NET Core Shared Framework Host help.
    
      --version                        Display .NET Core Shared Framework Host versi
    on.
    
    Options:
      --fx-version <version>           Version of the installed Shared Framework to
    use to run the application.
      --additionalprobingpath <path>   Path containing probing policy and assemblies
     to probe for.
    
    Path to Application:
      The path to a .NET Core managed application, dll or exe file to execute.
    
    If you are debugging the Shared Framework Host, set 'COREHOST_TRACE' to '1' in y
    our environment.
    
    To get started on developing applications for .NET Core, install the SDK from:
      http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

    1.2.输入以下命令,可以看到在hwapp目录下创建了一个控制台项目,cmd控制台窗口可看到“Hello World!"输出

    mkdir hwapp
    cd hwapp
    dotnet new dotnet restore dotnet run

    1.3 在vscode下调试项目

    用vscode打开hwapp文件夹;在扩展菜单那里找到C#扩展,并安装 ;点击调试菜单,并点击设置栏,选择.net core作为debug环境;打开program.cs文件,点击开始调试(F5),运行可看到调试控制台“Hello World!"输出(ps:默认调试当前打开文件)

    2.新建asp.net core程序(https://docs.microsoft.com/zh-cn/aspnet/core/getting-started)

    2.1 新建项目

    mkdir aspnetcoreapp

    cd aspnetcoreapp

    dotnet new –t web

    2.2 恢复包文件(ps:nuget包默认路径,在当前用户下的.nuget文件夹里,例如C:Userslindanyang.nuget,即我们加载的nuget包都在此路径下)

    dotnet restore

    此处可能由于被墙的原因,会报如下错误:

    error: Failed to retrieve information from remote source 'http://go.microsoft.co
    m/fwlink/?LinkID=206669/FindPackagesById()?id='Microsoft.AspNetCore.Diagnostics.
    EntityFrameworkCore''.
    error: Response status code does not indicate success: 301 (Moved Permanently).

    解决方法,在项目路径下添加 NuGet.config 文件,添加nuget源

    配置如下:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
    <add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="AspNetCI" value="https://www.myget.org/F/aspnetcirelease/api/v3/index.json" />
    </packageSources>
    </configuration>

    2.3 运行网站

    dotnet run 

     .net core版本配置问题也可能导致如下错误:

    The specified framework 'Microsoft.NETCore.App', version '1.1.0-preview1-001153-
    00' was not found.
    - Check application dependencies and target a framework version installed at:
    C:Program FilesdotnetsharedMicrosoft.NETCore.App
    - The following versions are installed:
    1.1.0
    - Alternatively, install the framework version '1.1.0-preview1-001153-00'.

    解决方法:

    根据dotnet命令获取.net core版本,修改project.json文件的.net core版本Version,如果nuget包的相关版本不对,也要修改:

      "userSecretsId": "aspnet-WebApplication-0799fe3e-6eaf-4c5f-b40e-7c6bfd5dfa9a",
    
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.1.0",
          "type": "platform"
        },

    保存后再次执行  dotnet restore   和  dotnet run 命令

    2.4 打开网站,如下图所示

    http://localhost:5000/

     2.5  项目文件下有README.md 文件,里面有web项目相关操作的教程网址,可以进一步学习

    2.6  vscode操作:vscode操作部分和控制台程序一致,有以下差异:

    (1)配置启动程序dll路径,即配置launch.json为如下路径:在name为.NET Core Launch (web)处配置program为${workspaceRoot}/bin/Debug/netcoreapp1.1/aspnetcoreapp.dll

     {
                "name": ".NET Core Launch (web)",
                "type": "coreclr",
                "request": "launch",
                "preLaunchTask": "build",
                "program": "${workspaceRoot}/bin/Debug/netcoreapp1.1/aspnetcoreapp.dll",
                "args": [],
                "cwd": "${workspaceRoot}",
                "stopAtEntry": false,
                "launchBrowser": {
                    "enabled": true,
                    "args": "${auto-detect-url}",
                    "windows": {
                        "command": "cmd.exe",
                        "args": "/C start ${auto-detect-url}"
                    },
                    "osx": {
                        "command": "open"
                    },
                    "linux": {
                        "command": "xdg-open"
                    }
                },
                "env": {
                    "ASPNETCORE_ENVIRONMENT": "Development"
                },
                "sourceFileMap": {
                    "/Views": "${workspaceRoot}/Views"
                }
            }

    (2)注意把之前用dotnet run命令启动的网站停掉:Ctrl+C,否则会报错

    (3)注意到ViewsShared\_Layout.cshtml文件引用的css和js文件并没有在文件夹里,需要另外下载加到路径里,之前dotnet run命令引用到的文件在

    <environment names="Staging,Production">标签里,而vscode是开发模式,引用的文件在<environment names="Development">里,而
    ~/lib/bootstrap/dist/css/bootstrap.css在项目文件夹里并没有,需要自己另外下载,其他的js文件也一样。
    <environment names="Development">
            <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
            <link rel="stylesheet" href="~/css/site.css" />
        </environment>
        <environment names="Staging,Production">
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/css/bootstrap.min.css"
                  asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
                  asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
            <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
        </environment>

    ps:其他详细配置和多项目程序待研究

    补充:多项目程序 :http://www.cnblogs.com/ldybyz/p/6483571.html

  • 相关阅读:
    jmeter上传和下载、webservice、数据库连接 -- 9
    jmeter cookies和token -- 8
    java 获得 微信 UserId
    让textarea根据文本的长度自动调整它的高度
    oracle 连接数据库并查询,返回List<Map<String, Object>> 数据
    POI 4.0 读取Excel
    excel (2)
    导出 doc
    sui Mobile 试玩
    oracle 与 前台 md5
  • 原文地址:https://www.cnblogs.com/ldybyz/p/6473156.html
Copyright © 2011-2022 走看看