简体   繁体   中英

WebAPI Controller method not showing in swagger

C# Azure Web Service API

I created a web service that I'm using a mobile app to hook up to. I created a default CRUD controller in the controllers container and all of those methods show (and works well) in 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. 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. 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:

    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() :

  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. Also make sure that your controller name is actually suffixed with .....Controller.

Who call your public static void Register(HttpConfiguration config) function?

From what I know you need to create Global.asax file After adding you will get file that look like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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