ASP.NET WebApi Routes and parameters types

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 00:09:35

问题


I have read many articles and similar questions but none of those decisions do not fit me. I have 3 methods:

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";
}

I need to configure route like:

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

How can I implement it?
I know this may be a duplicate (1, 2, 3), but I need help with it.

Thank you in advance


回答1:


Other solution is to add static segment to route:

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

Implementation:

    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";
    }

Route config:

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}
);



回答2:


Try this code:

    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"}
                );
            }
    }



回答3:


Configure your routes like this

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

Something like this:

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"}
            );



回答4:


So, you can not have two routes with same parameter count.

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

In this way, you can use in all your methods parameters type 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);
}

Route config:

 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 }
   );


来源:https://stackoverflow.com/questions/32112653/asp-net-webapi-routes-and-parameters-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!