入門(mén) 創(chuàng)建一個(gè)web項(xiàng)目
先創(chuàng)建一個(gè)web項(xiàng)目 基本可以運(yùn)行的程度 用postman進(jìn)行接口測(cè)試
.NET Framework 和 .NET Core 都可以創(chuàng)建 webAPI 這里用 .NET Framework 比較簡(jiǎn)單 。
啟動(dòng) Visual Studio,并從“開(kāi)始”頁(yè)中選擇“新建項(xiàng)目”。 或者,在 “文件” 菜單中,選擇“ 新建 ”,然后選擇“ 項(xiàng)目”。
在 “模板 ”窗格中,選擇 “已安裝的模板 ”并展開(kāi) “Visual C# ”節(jié)點(diǎn)。 在 Visual C# 下,選擇 “Web”。 在項(xiàng)目模板列表中,選擇 ASP.NET Web 應(yīng)用程序 或者直接在創(chuàng)建頁(yè)面搜索 “ASP.NET Web 應(yīng)用程序”
選擇 webAPI
創(chuàng)建好先關(guān)了 概述頁(yè)面
接下來(lái)要關(guān)注的文件夾只有 models 和 Controllers
在models 里添加Product 類
namespace ProductsApp.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
}
在controllers 里添加控制器 webapi 2 空 的
填寫(xiě)名稱有一定要在controller 前加上對(duì)應(yīng)數(shù)據(jù)類的名稱 --ProductsController
入門(mén) 定義方法
定義GetAllProducts () 和 GetProduct()方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
namespace WebApplication1.Controllers
{
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts()
{
return products;
}
public IHttpActionResult GetProduct(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}
}
接下來(lái)直接運(yùn)行就能看到我們的api了
如果報(bào)錯(cuò) localhost 拒絕了我們的連接請(qǐng)求 極有可能是端口繁忙,稍等一下就正常了
輸入 https://localhost:44378/api/Products 就可查看返結(jié)果
到此 web api 簡(jiǎn)單完成了
入門(mén) 操作返回結(jié)果
ASP.NET Web API如何將返回值從控制器操作轉(zhuǎn)換為 HTTP 響應(yīng)消息
。
如果操作返回 HttpResponseMessage,Web API 使用 HttpResponseMessage 對(duì)象的屬性來(lái)填充響應(yīng),將返回值直接轉(zhuǎn)換為 HTTP 響應(yīng)消息。
public class ValuesController : ApiController
{
public HttpResponseMessage Get()
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
response.Content = new StringContent("hello", Encoding.Unicode);
response.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = TimeSpan.FromMinutes(20)
};
return response;
}
}
IHttpActionResult在實(shí)際使用時(shí)可以用 System.Web.Http.Results 命名空間中定義的 IHttpActionResult 實(shí)現(xiàn)。 ApiController 類定義返回這些內(nèi)置操作結(jié)果的幫助程序方法。
其中定義了很多返回的類型可以直接調(diào)用
public IHttpActionResult Get (int id)
{
Product product = _repository.Get (id);
if (product == null)
{
return NotFound(); // Returns a NotFoundResult
}
return Ok(product); // Returns an OkNegotiatedContentResult
}
入門(mén) 幫助頁(yè) 和說(shuō)明文檔
添加 API 文檔
默認(rèn)情況下,幫助頁(yè)包含文檔的占位符字符串。 可以使用 XML 文檔注釋 創(chuàng)建文檔。 若要啟用此功能,請(qǐng)打開(kāi)文件 Areas/HelpPage/App_Start/HelpPageConfig.cs 并取消注釋以下行
config.SetDocumentationProvider(new XmlDocumentationProvider(
HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
下一步 勾選 , 輸入生成路徑
完成 這時(shí)給 controller上的方法寫(xiě)/// ///注釋 會(huì)顯示在幫助文檔上
/// <summary>
/// 方法名和請(qǐng)求方式是一一對(duì)應(yīng)的
/// </summary>
public IHttpActionResult GetEmployee(int id)
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-708614.html
路由
https://blog.csdn.net/qq_43886548/article/details/131083612文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-708614.html
到了這里,關(guān)于C# webAPI 精解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!