ASP. NET Web Api Practice Series (i) Self-hosted

Source: Internet
Author: User
Tags hosting web hosting

Starting today, the ASP. NET Web API (hereafter, sometimes referred to as the Web API) is studied. I will write a practical series of topics, not necessarily into a theoretical system, just encounter problems or experience, write down. Gain a deeper understanding of the ASP. NET Web API for a longer period of time. Here I use the VS2013 integrated development environment, if the version is not enough, you can use NuGet to download the Web API-related DLL.

Discuss the self-hosted web Api today.

The Web API inherits the benefits of WCF, and in addition to regular web hosting (IIS), it can be hosted in programs such as the Net console, WinForms, and so on. Here's a talk about hosting the Web Api in the console.

First, write the API method

New C # class library project Mycontrollers, referencing System.Web.Http.dll.

Write the API class file MyValuesController.cs:

using System.Web.Http; namespace mycontrollers{    publicclass  myvaluescontroller:apicontroller    {          Public string Get ()        {            return"OK";}}    }
Second, add the Homestay project

Add a console project MYCONSOLEAPISVR, referencing the Mycontrollers project, referencing the following 4 Web API-related DLLs:

    • System.Net.Http.dll
    • System.Web.Http.dll
    • System.Net.Formatting.Http.dll
    • System.Web.Http.SelfHost.dll

The main program is as follows:

1 usingSystem;2 usingSystem.IO;3 usingSystem.Web.Http;4 usingSystem.Web.Http.SelfHost;5 usingSystem.Reflection;6 7 namespaceMyconsoleapisvr8 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -Console.title ="Api Service"; -  the             varApidll = Path.Combine (AppDomain.CurrentDomain.BaseDirectory,"MyControllers.dll"); - Assembly.LoadFrom (apidll); -             varConfig =NewHttpselfhostconfiguration ("http://localhost:4588"); - CONFIG. Routes.maphttproute ( +Name"Defaultapi", -Routetemplate:"Api/{controller}/{id}", +DefaultsNew{id =routeparameter.optional}); A             using(varSVR =Newhttpselfhostserver (config)) at             { - SVR. OpenAsync (). Wait (); -Console.WriteLine ("API service is turned on! "); - console.readline (); -             } -              in         } -     } to}

The above code can be divided into several steps:

    1. Loads the DLL that contains the API controller method. (Line 16th)
    2. Add a service configuration and specify a service boarding address. (Line 17th)
    3. Adds a global default routing configuration. (第18-21 line)
    4. Define and open the service. (22nd, 24 lines)
Third, run the service and test

To run the Web API service:

and test in the browser:

ASP. NET Web Api Practice Series (i) Self-hosted

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.