how to create an asp.net web api project in visual studio 2017
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 CSS
, JavaScript
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}]
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2015-01-17 kill me heal me的链接