zoukankan      html  css  js  c++  java
  • Asp.NetCoreWebApi入门

    图文说明,注意流量.

    开发环境

    • Visual Studio 2019
    • .net core 2.x

    打开VS,建立项目



    建好之后就像下面这样

    继续再建立两个.net core类库项目分别是 ApiStudy.CoreApiStudy.Infrastructure

    • 右击解决方案,新建项目.
    • 选择 .NetCore类库项目.
    • 输入项目名.
    • ApiStudy.Core项目建立完成
    • 同样的方法再建立ApiStudy.Infrastructrue 项目.
    • 完成之后如图
    • 然后设置依赖关系

    项目结构

    一个解决方案下三个项目:

    • Xxxx.Core
      放一些核心的东西,比如 Entity(实体) 类和一些接口
    • Xxxx.Infrastructure
      放一些数据库连接之类(DbContext)的和一些服务
    • Xxxx.Api
      客户端项目

    修改 StartUp 类代码

    namespace ApiStudy.api
    {
        using Microsoft.AspNetCore.Builder;
        using Microsoft.AspNetCore.Hosting;
        using Microsoft.Extensions.DependencyInjection;
    
        public class Startup
        {
            public IConfiguration Configuration { get; }
    
            public StartupDevelopment(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
            }
    
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                app.UseDeveloperExceptionPage();
    
                app.UseRouting();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }
    

    注意, services.AddControllers();endpoints.MapControllers(); 方法成对出现, 这是.net core3.0中的最新写法

    ConfigureServices方法

    用来向容器中注册服务,注册好的服务可以在其他地方进行调用.

    Configure方法

    用来配置中间件管道,即如何响应http请求.

    为开发环境和生产环境配置不同的 Startup

    Startup 类修改为 StartupDevelopment

    新建 StartupProduction

    StartupProduction
    namespace MyBlog
    {
        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.Extensions.DependencyInjection;
        using Microsoft.Extensions.Hosting;
    
        public class StartupProduction
        {
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
            }
    
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                app.UseRouting();
    
                app.UseHttpsRedirection();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }
    

    修改 Program类的代码

    image
    配置使用的 Startup
    webBuilder.UseStartup(Assembly.GetExecutingAssembly().GetName().FullName);

    netcore会根据这里配置的环境是来选择使用哪个Startup类
    image

    Development -> StartupDevelopment
    Production -> StartupProduction

    新建一个Controller


    代码如下:

    namespace ApiStudy.Api.Controllers
    {
        using Microsoft.AspNetCore.Mvc;
    
        [Route("api/[controller]")]
        [ApiController]
        public class UserController:Controller
        {
            public IActionResult Get()
            {
                return Ok("Hello");
            }
        }
    }
    

    修改lauchSetting.json, 删除iis有关的部分, 如下:

    {
      "profiles": {
        "ApiStudy.api": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    F5运行

    浏览器访问 https://localhost:5001/api/user

  • 相关阅读:
    在网页上下载文件
    sql server 分离附加
    在vue中,ref属性与$refs对象的区别
    在ES6中,export default 和 export的区别
    element-ui之Table表格el-table标签
    element-ui之Form表单el-form标签
    使用Mybatis-Generator自动生成Dao,Entity,Mapping
    linux下普通用户与root的切换
    idea插件将下划线转驼峰形式
    利用wsdl2java工具生成webservice的客户端代码
  • 原文地址:https://www.cnblogs.com/Laggage/p/11105937.html
Copyright © 2011-2022 走看看