Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Debugging Web API Hello World
Message
De
02/05/2017 11:54:28
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Web Services
Divers
Thread ID:
01650768
Message ID:
01650776
Vues:
31
>>>Hi,
>>>
>>>I created a HelloWorld Web API solution in VS 2013. I left all the code automatically created by VS unchanged.
>>>On executing of this project the VS opens the browser and puts the following URL: "localhost:53757" which shows a descriptive ASP.NET page created by VS.
>>>Then when I add "/api/values" to this URL making it "localhost:53757/api/values" and the browser returns the array of two values (from the Controller class).
>>>
>>>I would like to understand better the WebApiConfig class which is in the APP_START folder. This class calls method Register and passes the HttpConfiguration config. I have two questions, please:
>>>
>>>1. Do I understand correctly that this class (WebApiConfig) and method are called ONLY ONE TIME when you start the Web API (project)? Why I think so: I set a debug point in this class, and the debug point is hit only once when I execute the proejct. When I add the "/api/values" to the localhost:53757 (making it localhost:53757/api/value) code in the class WebApiConfig does not fire and debut point is not hit.
>>>
>>>2. Below is the code of the WebApiConfig class. Could someone please explain it - in English - as simply as possible (if possible).
>>>
>>>
>>>   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 }
>>>            );
>>>        }
>>>    }
>>>
>>>
>>>Thank you in advance.
>>
>>Yup. Called only once to configure the routing for any subsequent webapi requests (that's why it's in the App_Start folder :-} )
>>
>>And, since you are using config.MapHttPAttributeRoutes() you shouldn't really need any other methods if you are using the RoutePrefix attribute on your ApiController and the Route attribute on the methods.
>
>Thank you for confirming my understanding on point 1.
>On point 2. I am using config.MapHttpAttributeRoutes() just because VS 2013 calls it (I really don't understand why). Is there a more up-to-date/better method to call?

No - it is the more up-to-date method. The older way (using config.Routes.MapHttpRoute() was less flexible - especially with REST based services. Using attribute routing means that with something like this:
    [RoutePrefix("myController")]
    public class MyApiController : ApiController
    {
        [Route("doIt")]
        public HttpResponseMessage DoSomething()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Worked");
        }
    }
you call it with /myController/doIt

See: https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform