資源簡(jiǎn)介
本資源對(duì)Web Api中的常用的POST\DELETE\PUT\GET動(dòng)作方法進(jìn)行了實(shí)例演示。
示例代碼中配備了詳細(xì)和全面的講解。讓用戶參照實(shí)例快速掌握Web Api在MVC中的使用。此外,對(duì)增刪改部份還結(jié)合了三層和EF進(jìn)行演示。
1.使用之前,需要更改主頁入口鏈接的主機(jī)號(hào)及端口號(hào)為本機(jī)。
2.在config中更改字符連接串的主機(jī)名。
代碼片段和文件信息
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請(qǐng)求方法之一的OPTIONS請(qǐng)求方法的主要用途有兩個(gè):
????????????//?1、獲取服務(wù)器支持的HTTP請(qǐng)求方法;也是黑客經(jīng)常使用的方法。
????????????//?2、用來檢查服務(wù)器的性能。例如:AJAX進(jìn)行跨域請(qǐng)求時(shí)的預(yù)檢,需要向另外一個(gè)域名的資源發(fā)送一個(gè)HTTP?OPTIONS請(qǐng)求頭,用以判斷實(shí)際發(fā)送的請(qǐng)求是否安全
????????????if?(Request.Headers.AllKeys.Contains(“Origin“)?&&?Request.HttpMethod?==?“OPTIONS“)
????????????{
????????????????Response.Flush();//緩存完畢后發(fā)送到客戶端
????????????}
????????}
????
}
}
評(píng)論
共有 條評(píng)論