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

时间:2016-07-17 22:45:06

标签: c# azure swagger

C#Azure Web服务API

我创建了一个网络服务,我使用移动应用程序来连接。我在控制器容器中创建了一个默认的CRUD控制器,所有这些方法都显示(并且运行良好)。

我在控制器中添加了一个新方法来执行CRUD中未涵盖的任务,并且它没有显示出招摇。例如:

    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
    }

即使我修改了控制器中的现有CRUD方法,它也会最终将其从swagger中移除。这似乎是某种映射问题,但除了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 }
        );
    }

为什么我不能更改方法的名称或在现有控制器中添加新方法?或者我应该说,我如何添加一个方法并让它显示在招摇?

3 个答案:

答案 0 :(得分:0)

将方法名称更改为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
    }

答案 1 :(得分:0)

将[Route]属性添加到方法中。 还要确保您的控制器名称后缀为..... Controller。

答案 2 :(得分:0)

谁调用了您的公共静态void Register(HttpConfiguration config)函数?

据我所知,您需要创建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)
    {

    }
}

只需在Application_Start函数上添加此行,然后实际上将调用Register函数

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