mardi 6 novembre 2018

c# web api service with multiple string parameters is not called

Hello I don't understand how to pass multiple string parameters to a c# web api web service.

I call the following url

http://mylocalhost/api/values/par1/par2/par3

but it gets executed the Get method with no parameters. Could you tell me what is wrong?

Here is my controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // GET api/values/5/6
        public string Get(int id, int id2)
        {
            return "value" + id2;
        }

        // GET api/values/string/string/string
        [HttpGet]
        [System.Web.Http.Route("api/values/f1/f2/f3")]
        public HttpResponseMessage Get(string id, string id2, string id3)
        {
            return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("test", System.Text.Encoding.UTF8, "application/json") };
        }


        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}

WebApiConfig has this code:

public static void Register(HttpConfiguration config) { // Servizi e configurazione dell'API Web

    // Route dell'API Web
    config.MapHttpAttributeRoutes();

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

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

    config.Routes.MapHttpRoute(
       name: "DefaultApi2",
       routeTemplate: "api/{controller}/{path}/{file}/{file2}",
       defaults: new { path = "", file = RouteParameter.Optional, file2 = RouteParameter.Optional  }
   );
}




Aucun commentaire:

Enregistrer un commentaire