Office中國(guó)論壇/Access中國(guó)論壇

 找回密碼
 注冊(cè)

QQ登錄

只需一步,快速開始

tag 標(biāo)簽: WebApi

相關(guān)帖子

版塊 作者 回復(fù)/查看 最后發(fā)表

沒有相關(guān)內(nèi)容

相關(guān)日志

分享 VS2012 ASP.NET WEB Api 跨域問題基礎(chǔ)解決方案
zhuyiwen 2021-10-13 12:02
參見: asp.net webapi 跨域問題解決 No 'Access-Control-Allow-Origin' header i_huazaizuiaiw的專欄-CSDN博客 通過(guò)Ajax調(diào)用web api路徑時(shí)報(bào)錯(cuò): No 'Access-Control-Allow-Origin' header is present on the requested resource. VS2012 自帶的web api并不能支持跨域訪問,如果需要,可以更改配置來(lái)實(shí)現(xiàn)。 1、更改Web.config文件的system.webServer節(jié)中,加上如下代碼 httpProtocol !--實(shí)現(xiàn)跨域-- customHeaders add name="Access-Control-Allow-Origin" value="*"/ add name="Access-Control-Allow-Headers" value="Content-Type,Token"/ add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/ /customHeaders /httpProtocol 2、然后需要配置Global.asax文件中,插入如下代碼: /// summary /// 配置Ajax跨域訪問 /// /summary /// param name="sender"/param /// param name="e"/param protected void Application_BeginRequest(object sender, EventArgs e) { if (Request.HttpMethod.ToUpper() == "OPTIONS") { Response.StatusCode = 200; Response.End(); } } 配置這兩個(gè)文件之后,web api就可以跨域訪問了。
個(gè)人分類: ASP.NET|1500 次閱讀|0 個(gè)評(píng)論
分享 ASP.NET WebApi 路由匹配及參數(shù)傳遞
zhuyiwen 2021-9-16 09:52
一、路由配置 針對(duì)ASP.NET WebAPI,我使用的開發(fā)工具是Visual Studio 2012。在新建的WebApi項(xiàng)目中默認(rèn)的路由配置為: public static class WebApiConfig { public static void Register( HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi" , routeTemplate: "api/{controller}/{id}" , defaults: new { id = RouteParameter .Optional } ); } } 從其中可以得到l默認(rèn)路由匹配模板: routeTemplate: "api/{controller}/{id}" 。其中 defaults: new { id = RouteParameter .Optional } 定義 id 為可選項(xiàng)。 二、添加控制器 在項(xiàng)目中添加一個(gè)“ 包含讀/寫操作的 Web API 2 控制器 ”,命名為“ ValueController ”,即產(chǎn)生一個(gè) ValueController.cs 的文件,其內(nèi)容為 public class ValueController : ApiController { // GET api/value public IEnumerable string Get() { return new string string value) { } // PUT api/value/5 public void Put( int id, string value) { } // DELETE api/value/5 public void Delete( int id) { } } 這是一個(gè)控制器模板,其控制器的名稱為“ Value ”,它與“ ValueController ”是一種約定俗成的關(guān)系,一看便知,“ Value ”控制器的類為“ ValueController ”。 三、初步測(cè)試 運(yùn)行項(xiàng)目,在瀏覽器中訪問 http://localhost:65134/api/value,可得到如下結(jié)果:(注:使用Chrome瀏覽器/360極速瀏覽器) This XML file does not appear to have any style information associated with it. The document tree is shown below. ArrayOfstring xmlns:i=" http://www.w3.org/2001/XMLSchema-instance " xmlns=" http://schemas.microsoft.com/2003/10/Serialization/Arrays " string value1 /string string value2 /string /ArrayOfstring 而訪問 http://localhost:65134/api/value/5 得到的結(jié)果如下: This XML file does not appear to have any style information associated with it. The document tree is shown below. string xmlns=" http://schemas.microsoft.com/2003/10/Serialization/ " value /string 呵呵,可以看出,http://localhost:65134/api/value 路由到了 public IEnumerable string Get() 方法,而 http://localhost:65134/api/value/5 路由到了 public string Get( int id)。 測(cè)試表明, Web Api 的訪問方法為:方法 + 路徑,根據(jù)方法使用 路由模板進(jìn)行匹配,本例中的路由模板為 routeTemplate: "api/{controller}/{id}" 。 由于是在瀏覽器中的地址欄填入訪問地址,故方法為“ GET ”方法,訪問地址根據(jù)路由模板進(jìn)行訪問 Web Api,例如訪問 http://localhost:65134/api/value/5 http://localhost:65134 為網(wǎng)站地址 api 為模板中固定的 api value 為模板中的 controller 5 為模板中的 id,是可選項(xiàng) 注 :使用瀏覽器測(cè)試 Web Api 并不是一個(gè)好主意,它不太好測(cè)試其它諸如 POST/PUT/DELETE 方法。建議使用大名頂頂?shù)? Fiddler 神器,可以構(gòu)造參數(shù)和選擇方法。呵呵,個(gè)人認(rèn)為最好的測(cè)試辦法是 編寫一個(gè)網(wǎng)頁(yè),在網(wǎng)頁(yè)中使用 jQuery Ajax 編寫一段小程序在 Chrome瀏覽器/360極速瀏覽器中進(jìn)行測(cè)試,因?yàn)?Chrome 的調(diào)試工具箱非常強(qiáng)大。 四、路由機(jī)制 例如訪問 http://localhost:65134/api/value/5,首先 Web Api 匹配路徑中的 "api" 確定是 Web Api 訪問,再匹配 “value” 找到控制器 “ ValueController ”,再根據(jù)訪問方法“ GET ”,在控制器類中匹配定義為“ HttpGet ”的方法,在本例中有兩個(gè), public IEnumerable string Get() 和 public string Get( int id),最終根據(jù)“5”來(lái)匹配有參數(shù)的 public string Get( int id)方法。 它不是根據(jù)控制器方法名稱來(lái)匹配的 ( 是不是很郁悶? ) 注 :約定俗成,控制器中方法名稱以“ Get ”開頭的方法默認(rèn)為“ HttpGet ”方法,以此類推,“ Post ”開頭的方法默認(rèn)為“ HttpPost ”方法...,它們不必注明 / ...。否則,必須方法定義的上一行用 / ...注明方法。呵呵,ASP.NET Web Api 和 Mvc 中有很多約定俗成規(guī)定。
個(gè)人分類: ASP.NET|1823 次閱讀|0 個(gè)評(píng)論

QQ|站長(zhǎng)郵箱|小黑屋|手機(jī)版|Office中國(guó)/Access中國(guó) ( 粵ICP備10043721號(hào)-1 )  

GMT+8, 2025-7-13 18:59 , Processed in 0.059877 second(s), 14 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回頂部