Multiple parameters in a web api 2 get

前端 未结 3 618
遇见更好的自我
遇见更好的自我 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:53

    You don't need a special routing record to handle multiple parameters. The routing record you created would be looking for the following route

    /api/controller/Dan@dan.com/Dan/FunnyLastName/TheCoffeeShop

    but you are trying to pass in parameters, not specify a route.

    with this routing record:

    config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional, action = "DefaultAction" });
    

    the following GET endpoint:

    public HttpResponseMessage Get(int requestId = 0, string userName = null, string departmentName = null, bool includeCompleted = false)
        {
          //code
        }
    

    could be hit as :

     /api/controllername/?requestId=15&username=Dan
    

    or

    /api/controllername/?departmentName=SoftwareEngineering
    

    or any other combination of the parameters (or no parameters since they have default values)

    Since you have a "Named" action (GetId) instead of the default actions (GET,POST,PUT..), this complicates things a little bit and you would have to work out a custom route to handle the action name. The following is what I use for custom action names (id is required in this example)

    config.Routes.MapHttpRoute("ActionRoute", "api/{controller}/{action}/{id}");
    

    Your endpoint would have to explicitly accept one parameter with the name 'id'

      public HttpResponseMessage LockRequest(int id, bool markCompleted)
        {
            //code
        }
    

    This endpoint would be hit at the following route:

    /api/controllerName/LockRequest/id?markCompleted=true
    

    Following the RESTful spec, it is better to stay away from custom action names when possible. Most of the time you can get away with the normal HTTP verbs and just use named actions to manipulate existing items (hence why ID is required in my example). For your code you could just have two GET endpoints, one that takes a specific ID to get the item, one that returns all items (including ids) based on "search parameters".

    public HttpResponseMessage Get(int id)
    public HttpResponseMessage Get(int requestId = 0, string userName = null, string departmentName = null, bool includeCompleted = false)
    

    These would both be handled by the default routing record.

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2021-02-01 19:58
    public class Parameters
        {
            public int Param1 { get; set; }
            public string Param2 { get; set; }
        }
    

    and then in your controller method:

        [ActionName("DoSomething")]
        [HttpPost]
        public IHttpActionResult DoSomething(Parameters myParameters)
        {
            var x = myParameters.Param1;
            var y = myParameters.Param1;
           //do something else..
    
        }
    

    And build a ajax call like this:

    var request = {
                        Param1 : "1",
                        Param2 : "Mystring"
                    };
    
        function callToMethodController(request) {
            var deferred = $q.defer();
            $http.post('api/object/DoSomething', request)
                .success(function (data) {
                    deferred.resolve(data);
                }).error(function (error) {
                    deferred.reject('There was an error.');
                });
            return deferred.promise;
        }
    
    0 讨论(0)
提交回复
热议问题