https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

Create a Web API Project

1.新建项目

2.选择Empty,然后web api

You can also create a Web API project using the "Web API" template. The Web API template uses ASP.NET MVC to provide API help pages. I'm using the Empty template for this tutorial because I want to show Web API without MVC. In general, you don't need to know ASP.NET MVC to use Web API.

Adding a Model

Adding a Controller

If you have used ASP.NET MVC, you are already familiar with controllers. Web API controllers are similar to MVC controllers, but inherit the ApiController class instead of the Controller class.

You don't need to put your controllers into a folder named Controllers. The folder name is just a convenient way to organize your source files.

https://stackoverflow.com/questions/22589245/difference-between-mvc-5-project-and-web-api-project

Basically, a Web API controller is an MVC controller, which uses HttpMessageResponse as the base type of its response, instead of ActionResponse. They are the same in most other respects. The main difference between the project types is that the MVC Application project type adds web specific things like default CSSJavaScript files and other resources needed for a web site, which are not needed for an API.

MVC is used for creating web sites. In this case Controllers usually return a View (i.e. HTML response) to browser requests. Web APIs on the other hand are usually made to be consumed by other applications. If you want to allow other applications to access your data / functionality, you can create a Web API to facilitate this access. For example, Facebook has an API in order to allow App developers to access information about users using the App. Web APIs don't have to be for public consumption. You can also create an API to support your own applications. For example, we created a Web API to support the AJAX functionality of our MVC web site.

Microsoft changed the way they present the different templates. Now instead of using different templates for different project types, they encourage developers to mix ASP.NET technologies inside the same project as needed. Microsoft calls this vNext.

UPDATE: For ASP.NET Core, Web API has been integrated into the MVC 6 project type and the ApiController class is consolidated合并 into the Controller class. Further details at: https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

https://stackoverflow.com/questions/9494966/difference-between-apicontroller-and-controller-in-asp-net-mvc

Use Controller(mvc) to render your normal views. ApiController action only return data that is serialized and sent to the client.

Quote:

Note If you have worked with ASP.NET MVC, then you are already familiar with controllers. They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data.

ApiControllers are specialized in returning data. For example, they take care of transparently serializing the data into the format requested by the client. Also, they follow a different routing scheme by default (as in: mapping URLs to actions), providing a REST-ful API by convention约定.

You could probably do anything using a Controller instead of an ApiController with the some(?) manual coding. In the end, both controllers build upon the ASP.NET foundation. But having a REST-ful API is such a common requirement today that WebAPI was created to simplify the implementation of a such an API.

It's fairly simple to decide between the two:

if you're writing an HTML based web/internet/intranet application - maybe with the occasional AJAX call returning json here and there - stick with MVC/Controller.

If you want to provide a data driven/REST-ful interface to a system, go with WebAPI. You can combine both, of course, having an ApiController cater AJAX calls from an MVC page.

To give a real world example: I'm currently working with an ERP system that provides a REST-ful API to its entities. For this API, WebAPI would be a good candidate. At the same time, the ERP system provides a highly AJAX-ified web application that you can use to create queries for the REST-ful API. The web application itself could be implemented as an MVC application, making use of the WebAPI to fetch meta-data etc.

Deploy the asp.net web api to IIS

1.Create a new Application pool on IIS named as Chuck_WebApi

2.Create a new Application on IIS named as Chuck_WebApi, and use the created Application pool in step 1

use fiddler's composer test the url

test with wrong controller  http://localhost/Chuck_WebApi/api/product/1

request

GET http://localhost/Chuck_WebApi/api/product/1 HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 0

response

HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 17 Jan 2019 08:58:52 GMT
Content-Length: 197

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/Chuck_WebApi/api/product/1'.","MessageDetail":"No type was found that matches the controller named 'product'."}

test the root path

request

GET http://localhost/Chuck_WebApi/ HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 0

response

HTTP/1.1 403 Forbidden
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Thu, 17 Jan 2019 09:14:25 GMT
Content-Length: 5213

只截取了一部分回复

<title>IIS 10.0 Detailed Error - 403.14 - Forbidden</title

test the products

request

GET http://localhost/Chuck_WebApi/api/products HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 0

response

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 17 Jan 2019 09:23:39 GMT
Content-Length: 182

[{"Id":1,"Name":"Tomato Soup","Category":"Groceries","Price":1.0},{"Id":2,"Name":"Yo-yo","Category":"Toys","Price":3.75},{"Id":3,"Name":"Hammer","Category":"Hardware","Price":16.99}]

how to create an asp.net web api project in visual studio 2017的更多相关文章

  1. NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project

    https://blog.rsuter.com/nswag-tutorial-integrate-the-nswag-toolchain-into-your-asp-net-web-api-proje ...

  2. [转]Web API Introduction to OData Services using ASP.NET Web API

    本文转自:http://mahedee.net/tag/web-api/ What is OData? OData Stands for Open Data Protocol. It is a dat ...

  3. OAuth Implementation for ASP.NET Web API using Microsoft Owin.

    http://blog.geveo.com/OAuth-Implementation-for-WebAPI2 OAuth is an open standard for token based aut ...

  4. Visual Studio 2017 and Swagger: Building and Documenting Web APIs

    Swagger是一种与技术无关的标准,允许发现REST API,为任何软件提供了一种识别REST API功能的方法. 这比看起来更重要:这是一个改变游戏技术的方式,就像Web服务描述语言一样WSDL( ...

  5. Getting Started with ASP.NET Web API 2 (C#)

    By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print   Download Com ...

  6. Enabling Cross-Origin Requests in ASP.NET Web API 2

    Introduction This tutorial demonstrates CORS support in ASP.NET Web API. We’ll start by creating two ...

  7. 【ASP.NET Web API教程】1.1 第一个ASP.NET Web API

    Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson ...

  8. [转] JSON Web Token in ASP.NET Web API 2 using Owin

    本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...

  9. JSON Web Token in ASP.NET Web API 2 using Owin

    In the previous post Decouple OWIN Authorization Server from Resource Server we saw how we can separ ...

随机推荐

  1. 项目分析(GS,NET,NGP关系)

    看了两天,这三者之间的关系好像是这样的,因为GS和net在同一台机器上,所以用共享内存通信,毕竟共享内存通信是最快的进程间通信的方式,而NGP是属于客户端的,Net是属于服务器的,他与Net进程是基于 ...

  2. ORA-00257错误的解决办法

    author: headsen  chen date: 2018-04-17  11:12:39 notice:个人原创,转载请注明作者和出处,否则依法追击法律责任. 1,oracle数据库正常使用中 ...

  3. 网络模型+三次握手+四次挥手+DNS+HTTPS

    网络模型+三次握手+四次挥手+DNS+HTTPS 这篇文章十分精华,所以整理一下: 一.网络模型 OSI七层模型,和TCP/IP五层模型(更为普遍) TCP/IP 协议集: 二.TCP协议(传输层)建 ...

  4. Spoken English Practice( Believe it or not, I don't need to make believe its a big deal. (believe,deal, You don't say))

    音标复习                                                绿色:连读:红色:略读:蓝色:浊化:橙色:弱读 口语蜕变(2017/6/25) Sorry, t ...

  5. 【转】spring和springMVC的面试问题总结

    1.Spring中AOP的应用场景.Aop原理.好处? 答:AOP--Aspect Oriented Programming面向切面编程:用来封装横切关注点,具体可以在下面的场景中使用: Authen ...

  6. 2015-03-22——js常用的Array方法

    Array array.concat(item...);  //产生一个新数组如果item,是一个数组,那么它的每个元素会被分别添加(浅复制,只解析一层).示例:var a = [1, 3, 4];v ...

  7. Making the Grade---poj3666(类似离散化+dp)

    题目链接:http://poj.org/problem?id=3666 题意是给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? ///用dp[i][j]表示 ...

  8. Python高级教程-迭代

    Python中的迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过for...in ...

  9. linux c编程:记录锁

    记录锁相当于线程同步中读写锁的一种扩展类型,可以用来对有亲缘或无亲缘关系的进程进行文件读与写的同步,通过fcntl函数来执行上锁操作.尽管读写锁也可以通过在共享内存区来进行进程的同步,但是fcntl记 ...

  10. C# OpenFileDialog 的使用方法

    OpenFileDialog openFileDialog = new OpenFileDialog(); //打开的文件选择对话框上的标题 openFileDialog.Title = " ...