简体   繁体   English

WebAPI控制器方法没有显示出招摇

[英]WebAPI Controller method not showing in swagger

C# Azure Web Service API C#Azure Web服务API

I created a web service that I'm using a mobile app to hook up to. 我创建了一个Web服务,我正在使用移动应用程序来连接。 I created a default CRUD controller in the controllers container and all of those methods show (and works well) in swagger. 我在控制器容器中创建了一个默认的CRUD控制器,所有这些方法都在swagger中显示(并且运行良好)。

I added a new method in the controller to perform a task not covered in the CRUD and it's not showing in swagger. 我在控制器中添加了一个新方法来执行CRUD中未涵盖的任务,并且它没有显示出来。 Example: 例:

    public async Task<IHttpActionResult> test(tblProfileList test)
    {
        // just testing to see if this method shows in swagger
        tblProfileList ProfileList = await db.tblProfileLists.FindAsync("10");
        return NotFound(); // yes, 10 will never be found but that's not the point of this test
    }

Even if I modify an existing CRUD method in the controller, it will end up removing it from swagger. 即使我修改了控制器中的现有CRUD方法,它也会最终将其从swagger中删除。 It seems like a mapping issue of some sort but I can't see where the methods are registered other than in the WebApiConfig.cs: 它似乎是某种映射问题,但除了WebApiConfig.cs之外,我无法看到方法的注册位置:

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

Why can't I change the names of the methods or add a new method in an existing controller? 为什么我不能更改方法的名称或在现有控制器中添加新方法? Or should I say, how can I add a method in and have it show in swagger? 或者我应该说,我如何添加一个方法并让它显示在招摇?

Change the name of the method to GetTest() : 将方法的名称更改为GetTest():

  public async Task<IHttpActionResult> GetTest(tblProfileList test)
    {
        // just testing to see if this method shows in swagger
        tblProfileList ProfileList = await db.tblProfileLists.FindAsync("10");
        return NotFound(); // yes, 10 will never be found but that's not the point of this test
    }

Add the [Route] attribute to the method. 将[Route]属性添加到方法中。 Also make sure that your controller name is actually suffixed with .....Controller. 还要确保您的控制器名称后缀为..... Controller。

Who call your public static void Register(HttpConfiguration config) function? 谁调用你的public static void Register(HttpConfiguration config)函数?

From what I know you need to create Global.asax file After adding you will get file that look like this: 据我所知,您需要创建Global.asax文件添加后,您将获得如下所示的文件:

public class Global
{

    protected void Application_Start(object sender, EventArgs e)
    {

    }

    protected void Session_Start(object sender, EventArgs e)
    {

    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {

    }

    protected void Application_Error(object sender, EventArgs e)
    {

    }

    protected void Session_End(object sender, EventArgs e)
    {

    }

    protected void Application_End(object sender, EventArgs e)
    {

    }
}

Just add this line on Application_Start function and then the Register function will actually be called 只需在Application_Start函数上添加这一行,然后实际调用Register函数

protected void Application_Start(object sender, EventArgs e)
{
    GlobalConfiguration.Configure(YouClassName.Register);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM