Web api--Create Webapi

Source: Internet
Author: User

Tag: ble TE pat means init returns to browser star round

Web api--Create Webapi

Method in Win10 + VS2017 (MVC5) test pass

1. Establish WEBAPI project:

Select the menu "file-New eye-" web-"ASP. NET Web Application"

Enter the project name and location, OK, go to the template Selection page.

Select Web API template (add support for MVC when selecting the Web API)

2. Add Apicontroller

Looking at the catalog structure of the production, it is found that the structure of WEBAPI is very similar to the normal MVC program structure.

Default VS Creates a sample controller under the Controllers folder: Valuescontroller

By looking at the controller code, you find the main difference between the WEBAPI controller and the MVC controller:

1) WEBAPI controller class inherits from Apicontroller class

2) The WebAPI method returns the raw data, not the view.

(1) When adding your own controller, just in the reference example, under the Controllers folder, create a new class that inherits from Apicontroller

Apicontroller under Namespace System.Web.Http

Controllers generally end with a controller as agreed, such as : Action Controller

3. Routing and Methods in the Web API

We can view and modify WebAPI's routes (and of course other configurations) within the Webapiconfig file in the App_start folder.

Within the file, there is a default routing configuration

As you can see, WebAPI routing is very similar to the MVC rationale, but there is a clear difference between the {action} directive in Web API routing.

This is because the Web API default action is dispatched using the HTTP verb that is requested .

That is , there are methods in the controller that begin with the common Http verb (Get, Post, Put, Delete, Head, Patch, Options), which can match the request of the corresponding verb.

For example, the Get method in the Valuescontroller controller

When the client requests the controller in a get way, the Web API automatically matches the Get method.

For a method where the name does not match the common verb, the Web API supports the POST request by default, and you need to decorate it with the [Http ...] feature in the method.

Such as:

At this point, the default routing configuration does not apply, and you need to add a custom routing configuration in the Webapiconfig file (mainly to add {action} attributes to support the method of mismatched verbs)

Once this is done, you can invoke the API method in this project .

The calling method is the same as the normal MVC method call.

The reason is that it can be called in this project because cross-domain operations are required when other projects are called, and the settings need to be called across domains.

4. Cross-domain calls in the Web API

To make the Web API callable across domains, you first need to add a Microsoft.AspNet.Cors reference to your project. You can use NuGet to install Microsoft.AspNet.Cors in your project

Note: You need to refer to Microsoft.AspNet.WebApi.Cors in Owin

After the Microsoft.AspNet.Cors installation is successful, you need to add a cross-domain method configuration in the Webapiconfig file

The most important Add code

Config. Enablecors (New Enablecorsattribute ("*", "*", "*"));

Enablecorsattribute in the namespace System.Web.Http.Cors (within the newly installed Microsoft.AspNet.Cors)

The parameters, which represent the configuration of a method that can be called across domains (all "*" means that all methods can be called across domains)

Web API creation, configuration is complete, everywhere.

However, there are times when cross-domain configuration is complete and you cannot make cross-domain calls in other projects. This may be the reason why the browser is not supported. Browser support can be referenced (from the Internet Ba LA)

At this point, you need to set up cross-domain support in the JS that invokes the API.

Fortunately, JQuery provides a simple method that only needs to be set via JQ: JQuery.support.cors = True

, JQuery.support.cors = True to set the browser's support for cross-domain calls.

The Ajax method is called the method in the controller (IPAddress in the figure) (GetIP, parameter 0), you can see that the calling method is the same as the MVC method.

Reprint:https://www.cnblogs.com/xtblog/p/8099382.html

At this point, a simple Web API has been created.

Web api--Create Webapi

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.