ASP. NET Web API practice series 01, hosted in ASP. NET Web Form mode, apiform

Source: Internet
Author: User

ASP. NET Web API practice series 01, hosted in ASP. NET Web Form mode, apiform

Create an empty ASP. NET Web Form project.

 

Right-click a project, add a new project, and create a Web API controller class, TestController.

 

Delete the default content of TestController and write it as follows:

using System.Web.Http;
namespace WebApplication1
{
    public class TestController : ApiController
    {
        [AcceptVerbs("Get")]
        public string SayHello()
        {
            return "Hello World From ASP.NET Web API";
        }
    }
}

 

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

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

 

Next, we need to register a template similar to "api/{controller}/{action}" to the routing set of ASP. NET.

 

Create the Globa. asax file and write it as follows:

using System.Web.Routing;
using System.Web.Http;
namespace WebApplication1
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
// Format: webapi/Test/SayHello
            RouteTable.Routes.MapHttpRoute("demo", "webapi/{controller}/{action}");
        }
    }
}

 

We put the project on IIS instead of IISExpress for debugging. Right-click Project -- Property -- Web and set it as follows:

 

Save settings. In the following prompt box, click "yes ".

 

Then, an IIS Site is added to IIS.

 

Open the browser and enter http: // localhost/WebApplication1/webapi/Test/SayHello.

 

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

 

Open Fiddler. If you set the "Accept" of the request to "application/html", we can get the content in the correct format.

 

The returned result is status code 200.


The concepts of web forms, Html, and their respective features in aspnet, and their differences

Asp.net form and html form are both the same
However, when writing server code, you can add server controls and call these controls in the background. Of course, javascript can be called regardless of the server control or html control.

You can write html forms and asp.net forms on the same page.
Click View Source Code and you will find that they are all the same.

The difference between 'aspnet Web service' and 'aspnet website'

Asp.net is A. Net-based website.
Asp.net web Service is a. NET-based service. It is a web application that can expose an API interface to the outside world and can be called through the WEB.
An empty website is a website built by Vs studio, but it does not have any substantive content.

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.