-1

我是 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)
        {

        }    
    }
}
4

1 回答 1

0

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

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

于 2015-05-28T09:16:58.883 回答