ASP.NET Core 1.0 中使用 Swagger 生成文档
2016-02-21 17:52
1126 查看
之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1.0中同样也支持。
依赖包
项目属性选中“生成时生成输出”选项用来生成XML注释
Startup类
访问:http://localhost/swagger/ui/index.html
依赖包
"dependencies": { "Swashbuckle.SwaggerGen": "6.0.0-rc1-final", "Swashbuckle.SwaggerUi": "6.0.0-rc1-final" }
项目属性选中“生成时生成输出”选项用来生成XML注释
Startup类
public class Startup { //private readonly string pathXml = @"\\Mac\Home\Desktop\asp.net core\learn_asp.net core 1.0\artifacts\bin\SwaggerForASP.NETCore\Debug\dnx451\SwaggerForASP.NETCore1.0.xml"; private readonly string pathXml = @"C:\Users\irving\Desktop\asp.net core\LearningASP.NETCore\artifacts\bin\LearningASP.NETCore\Debug\dnx451\LearningASP.NETCore.xml"; public Startup(IHostingEnvironment env) { // Set up configuration sources. var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; set; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); services.AddSwaggerGen(); services.ConfigureSwaggerDocument(options => { options.SingleApiVersion(new Info { License = new License { Name = "irving", Url = @"http://cnblogs.com/irving" }, Contact = new Contact { Name = "irving", Email = "zhouyongtao@outlook.com", Url = @"http://cnblogs.com/irving" }, Version = "v1", Title = "ASP.NET Core 1.0 WebAPI", Description = "A Simple For ASP.NET Core 1.0 WebAPI", TermsOfService = "None" }); options.OperationFilter(new ApplyXmlActionComments(pathXml)); }); services.ConfigureSwaggerSchema(options => { options.IgnoreObsoleteProperties = true; options.DescribeAllEnumsAsStrings = true; options.ModelFilter(new ApplyXmlTypeComments(pathXml)); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseIISPlatformHandler(); app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); app.UseSwaggerGen(); app.UseSwaggerUi(); } // Entry point for the application. public static void Main(string[] args) => WebApplication.Run<Startup>(args); }
OrdersController类
[Route("api/orders")] public class OrdersController : Controller { [HttpGet] [Route("info")] public async Task<ActionResult> Info() { return await Task.Run(() => { return Json(new { name = "irving", age = 25 }); }).ContinueWith(t => t.Result); } // GET: api/values [HttpGet] public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 [HttpGet("{id}")] public string Get(int id) { return "value"; } // POST api/values [HttpPost] public void Post([FromBody]string value) { } // PUT api/values/5 [HttpPut("{id}")] public void Put(int id, [FromBody]string value) { } // DELETE api/values/5 [HttpDelete("{id}")] public void Delete(int id) { } }
访问:http://localhost/swagger/ui/index.html
相关文章推荐
- ASP.NET和ASP的区别
- Metasploit渗透测试实验报告
- Metasploit 整理笔记
- ASP.NET 4.0尚未在 Web 服务器上注册
- 【我的ASP.NET学习笔记】个人博客系统-Request.Params["cid"]--知识
- 【ASP.Net】使用自定义服务器控件
- 同时向主表和从表里面导入execl数据 (asp.net webform)
- ASP.NET中的多线程整理
- 【踩坑经历】一次Asp.NET小网站部署踩坑和解决经历
- 【原创】Aspose.Words组件介绍及使用—基本介绍与DOM概述
- Asp.Net Mvc应用的程序结构
- Asp.net MVC 的八个扩展点
- ASP.NET ViewState详解
- AspectJ基础学习之三HelloWorld(转载)
- AspectJ基础学习之二搭建环境(转载)
- AspectJ基础学习之一简介(转载)
- asp.net无刷新上传(带预览)
- ASP.NET 中通过Form身份验证 来模拟Windows 域服务身份验证的方法
- [译] ASP.NET 生命周期 – ASP.NET 上下文对象(八)
- ASP.NET MVC:通过 FileResult 向 浏览器 发送文件