【问题标题】:How can I add new method in Swagger UI?如何在 Swagger UI 中添加新方法?
【发布时间】:2015-08-10 14:14:25
【问题描述】:

我是 Swagger API 的新手,在 ASP.Net 中也是如此,我想知道如何在 UI 上添加新的 HTTP 方法(例如 GET、POST、PUT、DELETE)。它仅包含 6 个默认方法。我想添加另一个 GET 方法。那么,有什么帮助吗?

控制器

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace DemoSwagger.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "Cat", "Dog", "Bear" };
        }

        // GET api/values/5
        public string Get(int id, string sm, string BCID)
        {
            if (id == 1)
            {
                return "Cat";
            }
            else if (id == 2)
            {
                return "Dog";
            }
            else if (id == 3)
            {
                return "Bear";
            }
            else
            {
                return "Out of Range";
            }    
        }

        // POST api/values
        public void Post([FromBody]string value)
        {


        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {

        }

        // DELETE api/values/5
        public void Delete(int id)
        {

        }    
    }
}

【问题讨论】:

  • 你的代码是什么样的?信息太少我们无法回答。
  • 我已经编辑了这个问题。希望它更清楚

标签: asp.net-mvc-3 swagger-ui swagger-2.0


【解决方案1】:

Swagger 仅代表您的 API“签名”。您不会向 Swagger 添加方法,而是向 API 添加方法,然后 Swagger 会显示它。

您只需将方法添加到您的 API,它们就会显示出来。如果没有,请使用 [Get] 和/或 [HttpGet] 装饰它们(取决于您的 Swagger 和 MVC API 版本)。

【讨论】:

  • 我在 WebApiConfig.cs 中编辑了一行代码,然后我将 [HttpGet]/[HttpPost] 放在我的控制器上,然后我现在可以添加新方法了。起初,我认为这与招摇。谢谢你这有很大帮助
  • 如果它解决了您的问题,请将答案标记为已接受:) 此外,您可以编辑您的问题以提供您找到的解决方案(用于 WebApiConfig)。
  • @JeraBalais 您在 WebApiConfig.cs 中添加了哪一行?
猜你喜欢
  • 2020-08-31
  • 2019-03-23
  • 1970-01-01
  • 2021-03-24
  • 1970-01-01
  • 2022-11-14
  • 2018-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多