"Asp.net5" Web API Controller class Learning

Source: Internet
Author: User

The Web API Controller class is a template type that creates a restful interface class when you add a new item in a project of the ASP. NET Web application type in VS2015.

Why choose it not to choose MVC Controller class?

Because we don't want it to be related to view, no view at all;

For the master is actually the same, inheriting classes are the same, but the specific implementation is not the same;

For example, if someone else has provided a complete code like this, it doesn't matter which template to choose.

The sample code is as follows:

Using system;using system.collections.generic;using system.linq;using system.threading.tasks;using microsoft.aspnet.mvc;//for more information in enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/ ? Linkid=397860namespace Dotnethelloworld.        controllers{[Route ("Api/[controller]")] public class Valuescontroller:controller {//Get:api/values  [HttpGet] public ienumerable<string> Get () {return new string[] {"value1", "value2"        };         }//Get API/VALUES/5 [HttpGet ("{ID}")] public string get (int id) {return "value";        }//Post api/values [httppost] public void post ([frombody]string value) {}         Put API/VALUES/5 [Httpput ("{ID}")] public void Put (int id, [frombody]string value) {} Delete API/VALUES/5 [Httpdelete ("{ID}")] public void Delete (int id) {}}}

To add a method:

Right-click on the project name, add New item


Select Server side on the left, middle selection: Web API Controller Class, enter the name you want to use at the name


Browse Calls:

VS2015 window Active state, press F5 key to enter the debug state, will open a default browser window, the address is similar to: http://localhost:3753

Note: The rear port is random;

Rest-style API We access the only get-type interface requests that can be tested through the browser's address bar,

Let's take a look at the comment for the GET () method://Get:api/values

This tells us that this method can be accessed through api/values, access to addresses such as: http://localhost:3753/api/values

Let's take a look at the comment for the GET (int id) method://GET:API/VALUES/5

This tells us that this method can be accessed through API/VALUES/5, access to addresses such as: HTTP://LOCALHOST:3753/API/VALUES/5

The following 5 is passed to the value of the corresponding int ID, the type needs to match, the default is 0;




"Asp.net5" Web API Controller class Learning

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.