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.