資源簡介
本資源對Web Api中的常用的POST\DELETE\PUT\GET動作方法進行了實例演示。
示例代碼中配備了詳細和全面的講解。讓用戶參照實例快速掌握Web Api在MVC中的使用。此外,對增刪改部份還結(jié)合了三層和EF進行演示。
1.使用之前,需要更改主頁入口鏈接的主機號及端口號為本機。
2.在config中更改字符連接串的主機名。
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Web.Http;
using?System.Web.Mvc;
using?System.Web.Optimization;
using?System.Web.Routing;
namespace?ApiDemo
{
????public?class?WebApiApplication?:?System.Web.HttpApplication
????{
????????protected?void?Application_Start()
????????{
????????????AreaRegistration.RegisterAllAreas();
????????????GlobalConfiguration.Configure(WebApiConfig.Register);
????????????FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
????????????RouteConfig.RegisterRoutes(RouteTable.Routes);
????????????BundleConfig.RegisterBundles(BundleTable.Bundles);
????????}
????????protected?void?Application_BeginRequest(object?sender?EventArgs?e)
????????{
????????????//origin頭主要是跨域相關(guān)
????????????//作為HTTP請求方法之一的OPTIONS請求方法的主要用途有兩個:
????????????//?1、獲取服務(wù)器支持的HTTP請求方法;也是黑客經(jīng)常使用的方法。
????????????//?2、用來檢查服務(wù)器的性能。例如:AJAX進行跨域請求時的預(yù)檢,需要向另外一個域名的資源發(fā)送一個HTTP?OPTIONS請求頭,用以判斷實際發(fā)送的請求是否安全
????????????if?(Request.Headers.AllKeys.Contains(“Origin“)?&&?Request.HttpMethod?==?“OPTIONS“)
????????????{
????????????????Response.Flush();//緩存完畢后發(fā)送到客戶端
????????????}
????????}
????
}
}
評論
共有 條評論