ASP.NET WebApi路由和参数类型

时间:2015-08-20 07:58:45

标签: c# asp.net-web-api routing

我读过很多文章和类似的问题,但这些决定都不适合我。 我有3种方法:

public string GetA()
{
   return "Hello from GetA";
}
public string GetB(int id)
{
   return "Hello from GetB";
}
public string GetC(sting all)
{
   return "Hello from GetC";
}

我需要配置路由,如:

1.http://localhost:63087/api/Test/
2.http://localhost:63087/api/Test/all
3.http://localhost:63087/api/Test/1
4.http://localhost:63087/api/Test/1/all

我该如何实施?
我知道这可能是重复的(123),但我需要帮助。

提前谢谢

4 个答案:

答案 0 :(得分:0)

试试这段代码:

    public static class WebApiConfig
    {
       public static void Register(HttpConfiguration config)
       {           
                config.SuppressDefaultHostAuthentication();
                config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

                // Web API routes
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/Test",
                    defaults: new { controller = "Test", action = "GetA"}
                );

                config.Routes.MapHttpRoute(
                    name: "WithID",
                    routeTemplate: "api/Test/{id}",
                    defaults: new { controller = "Test", action = "GetB", id = UrlParameter.Optional }
                );

                config.Routes.MapHttpRoute(
                    name: "ALL",
                    routeTemplate: "api/Test/all",
                    defaults: new { controller = "Test", action = "GetC"}
                );
            }
    }

答案 1 :(得分:0)

像这样配置你的路线

http://localhost:63087/api/Test
http://localhost:63087/api/Test/1
http://localhost:63087/api/Test/1/all

这样的事情:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/Test",
                defaults: new { controller = "Test", action = "GetA"}
            );

            config.Routes.MapHttpRoute(
                name: "Route2",
                routeTemplate: "api/Test/{id}",
                defaults: new { controller = "Test", action = "GetB" }
            );

            config.Routes.MapHttpRoute(
                name: "Route3",
                routeTemplate: "api/Test/{id)/{all}",
                defaults: new { controller = "Test", action = "GetC"}
            );

答案 2 :(得分:0)

因此,您不能有两条具有相同参数计数的路径。

2.http://localhost:63087/api/Test/all
3.http://localhost:63087/api/Test/1

通过这种方式,您可以在所有方法中使用参数类型string

public string GetA()
{
   return "Hello from GetA";
}
public string GetB(string id, string all = "")
{
   if (id.Equals("all") || all.Equals("all"))
   {
       return "Hello all from GetB";
   }
   return string.Format("Hello {0} from GetB", id);
}

路线配置:

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

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

答案 3 :(得分:0)

其他解决方案是添加静态段到路由:

1.http://localhost:63087/api/Test/
2.http://localhost:63087/api/all/Test/
3.http://localhost:63087/api/Test/1
4.http://localhost:63087/api/all/Test/1

实现:

    public string GetA()
    {
        return "Hello from GetA";
    }
    public string GetB(int id)
    {
        return "Hello from GetB";
    }

    [Route("api/all/{controller}/{id}")]
    [Route("api/all/{controller}")]
    public string GetC(int id= 0)
    {
        return "Hello from GetC";
    }

路线配置:

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

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