zoukankan      html  css  js  c++  java
  • .net core webapi搭建(1)

    创建一个webapi项目

    修改launchSettings.json

    将launchSettings.json中的IIS启动删掉。别问我为啥  原因就是IISEXPRESS有时候需要我手动重启。我嫌麻烦。

    删除后的代码就 变成了这个样子

    {
      "profiles": {
        "MsgWebApi": {
          "commandName": "Project",
          "launchBrowser": true,
          "launchUrl": "api/values",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          "applicationUrl": "http://localhost:62827/"
        }
      }
    }
    

    F6运行一下,修复一下DLL和看看有没有错误

    为Web Api添加Swagger帮助页面

    完全依照官方文档安装swagger即可: https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio

    这部分就写了。写一个通过nuget安装的方法。

    Install-Package Swashbuckle.AspNetCore

    如下图

    在Startup的ConfigureServices注册并配置Swagger, 然后在StartUp的Configure方法使用Swagger中间件:

            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
    
                services.AddSwaggerGen(c =>
                 {
                     c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
                 });
            }
    
            // 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();
                }
    
                // Enable middleware to serve generated Swagger as a JSON endpoint.
                app.UseSwagger();
    
                // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                });
    
    
                app.UseMvc();
            }
    

      好了 。第一次运行F5跑起来吧。

    http://localhost:62827/swagger/index.html  访问地址是:url地址+swagger+index.html看不懂的话。就看不懂吧。

    运行效果如下。

    后续

    .net core qq群:136724480

  • 相关阅读:
    2019-05-29 EL表达式中使用三目运算符
    2019-05-24 创建redis集群
    2019-05-24 Linux命令ps aux|grep XXX
    2019-05-24 编写批处理脚本;给权限;
    2019-05-24 网站"XXX"求把名为"cookie"的文件存放在你的计算机上,此文件可以
    挣值、预测技术
    挣值、预测
    进度网络计算
    NPV净现值
    Arguments
  • 原文地址:https://www.cnblogs.com/Extnet/p/9605559.html
Copyright © 2011-2022 走看看