Multiple parameters in a web api 2 get

前端 未结 3 619
遇见更好的自我
遇见更好的自我 2021-02-01 19:32

I want to make a web api that is passed 4 parameters.

Here is my route:

        config.Routes.MapHttpRoute(
            name: \"DefaultApi\",
                    


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 19:56

    Ensure you have default api route setting in WebApiConfig.cs file.

    public static class WebApiConfig
    {
        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 }
            );
    
            config.Routes.MapHttpRoute(
            name: "ContactApi",
            routeTemplate: "api/{controller}/{email}/{firstname}/{lastname}/{source}"
            );
        }
    }
    
    http://fakedomain.com/api/Contacts/GetId?email=user@domain.com&firstname=joe&lastname=shmoe&source=123
    

    Note : I replaced fakedomain with localhost and it works... (localhost/api/Contacts/GetId?email=user@domain.com&firstname=joe&lastname=shmoe&source=123)

提交回复
热议问题