ASP. NET Web API Practice series 01, hosted in ASP.

Source: Internet
Author: User

Create an empty ASP. NET Web form project.

Right-click the project, add New item, create Web API controller class, TestController.

Delete the TestController default content, write the following:

using System.Web.Http;
namespace WebApplication1
{
     Public class Testcontroller:apicontroller
    {
        [Acceptverbs ("Get")]
         Public string SayHello ()
        {
            return "Hello World from ASP. Web API";
        }
    }
}

In fact, when adding the TestController class, we added the following components by default:

1, System.Web.Http.dll
2, System.Web.Http.WebHost.dll
3, System.Net.Http.dll
4, System.Net.Http.Formatting

The next step is to register the template with a similar "api/{controller}/{action}" to the route collection of ASP.

Create the Globa.asax file and write the following:

using System.Web.Routing;
using System.Web.Http;
namespace WebApplication1
{
     Public class Global:System.Web.HttpApplication
    {
        protected void Application_Start (object sender, EventArgs e)
        {
            Compliant format: Webapi/test/sayhello
            RouteTable.Routes.MapHttpRoute ("demo", "Webapi/{controller}/{action}");
        }
    }
}

We put the project on IIS instead of debugging on iisexpress. Right-click the Project-property--web, set as follows:

To save the settings, jump out of the box below and click "Yes".

Then, you have one more IIS site on IIS.

Open Browser Input: Http://localhost/WebApplication1/webapi/Test/SayHello

Why is the returned format XML?
-Because of the Chrome browser's request header, the type of the "Accept" request is XML.

Open fiddler, if the request "Accept" is set to "application/html", we can get to the content of the format.

The returned result is status code 200.

ASP. NET Web API Practice series 01, hosted in ASP.

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.