ก่อนอื่นขอออกตัวก่อนว่าไม่ได้เขียน Web API อย่างเป็นกิจลักษณะ แต่ช่วงหลังต้องไปซัพพอร์ทชาวบ้านข้างนอกก็เริ่มความเห็นความสำคัญของ Web API ขึ้นมาทันตาเพราะมันเป็นเหมือนโครงสร้างพื้นฐานที่ทำให้คนอื่นสามารถเขาไปต่อเติมในงานของตนเองได้ตามใจชอบ ที่เป็นกันก็เช่น API ของ Google (maps, plus, docs เป็นต้น), Facebook, Twitter และเจ้าอื่น ๆ อีกมากมายที่ให้บริการทั้งแบบเปิดสำหรับคนทั่วไป และแบบปิดเฉพาะองค์กร โดยคำว่า API ย่อมาจาก Application Programming Interface ซึ่งเป็นคำกลาง ๆ สำหรับโปรแกรมที่เป็นตัวช่วยให้โปรแกรมอื่นทำงานได้ตามที่ต้องการ แล้วรวมคำว่า Web เป็น Web API ก็เป็น API ที่ทำงานแบบ Web หรือผ่าน HTTP ซึ่งก็มีหลายตัวที่นิยม ก็เป็น Web service (แล้ว Microsoft มาพัฒนาต่อก็เป็น WCF -- Windows Communication Foundation) และ RESTful (แล้ว Microsoft มาพัฒนาต่อเป็น Web API ที่เราจะพูดถึง)
ฟังชั่นใหม่ใน ASP.NET Web API 2.2
OData v4
เราเพิ่มการรองรับ OData v4 protocal ใน release นี้. มีบางฟังชั่นที่เพิ่ม/เปลี่ยน อยู่ตามรายการด้านล่าง:
- รวมกับ ODL UriParser
- รองรับ cast สำหรับ primitive types
- รองรับ cast() ใน $filter
- รองรับสำหรับการใช้ open complex type
- ยกเลิด EntitySetController และ AsyncEntitySetController
Attribute Routing Improvements
Attribute จัดการเส้นทรงตอนนี้ provides โดยการ extensibility point called IDirectRouteProvider, which allows full control over how attribute routes are discovered and configured. An IDirectRouteProvider is responsible for providing a list of actions and controllers along with associated route information to specify exactly what routing configuration is desired for those actions. An IDirectRouteProvider implementation may be specified when calling MapAttributes/MapHttpAttributeRoutes.
การปรับแต่ง IDirectRouteProvider จะง่ายขึ้นโดยการ extending จาก default implementation, DefaultDirectRouteProvider. เป็น class provides แยก overridable virtual methods เป็นการเปลี่ยน logic ของตรวจจับ attributes, การสร้าง route entries, และการตรวจจับ route prefix และ area prefix.
จากตัวอย่างสิ่งที่เราทำได้จาก extensibility point แบบใหม่:
- รองรับ inheritance จาก Route attributesตัวอย่าง:ถ้านี้เป็น request ที่ส่งมาแบบนี้ "/api/values/10" ข้อความที่ตอบกลับเมื่อทำงานสำเร็จจะ return "Success:10"
public class BaseController : ApiController { [Route("{id:int}")] public string Get(int id) { return "Success:" + id; } } [RoutePrefix("api/values")] public class ValuesController : BaseController { } config.MapHttpAttributeRoutes(new CustomDirectRouteProvider()); public class CustomDirectRouteProvider : DefaultDirectRouteProvider { protected override IReadOnlyList
GetActionRouteFactories(HttpActionDescriptor actionDescriptor) { return actionDescriptor.GetCustomAttributes (inherit: true); } } - โดยปกติมีการทำ route name สำหรับ attribute routes โดยตามความความสะดวกของคุณ. โดยปกติ, attribute routing จะไม่สามารถเปลี่ยนได้เองแบบอัตโนมัติในสร้าง names ของ attribute routes.
- ปรับต่าง attribute routes' route template ที่ central place เดียวก่อนทั้งหมดจะเป็นไปตาม route table.
Web API Client รองรับบน Windows Phone 8.1
คุณสามารถใช้ Web API Client NuGet package เพื่อ implement ตัว Web API client logic ของคุณเมื่อลูกค้าต้องพัฒนาบน Windows Phone 8.1 หรือ Universal App.
รู้จัก Issues และ Breaking Changes
ในส่วนนี้จะอธิบายให้รู้จัก issues และ breaking ที่เปลี่ยนใน the ASP.NET Web API 2.2.
OData v4
Model builder
Issue: Overloaded Functions ไม่สามารถเปิด FunctionImport
ถ้ามันมี 2 overloaded functions และมันเป็น FunctionImport ดังนั้นจะแสดงดัง requesting ด้านล่าง ~/GetAllConventionCustomers(CustomerName={customerName}) results in System.InvalidOperationException.
<Function Name="GetAllConventionCustomers" ReturnType="Collection(WebStack.QA.Test.OData.UnboundOperation.ConventionCustomer)" IsComposable="true" /> <Function Name="GetAllConventionCustomers" ReturnType="Collection(WebStack.QA.Test.OData.UnboundOperation.ConventionCustomer)" IsComposable="true"> <Parameter Name="CustomerName" Type="Edm.String" FixedLength="false" Unicode="false" /> </Function> ... <FunctionImport Name="GetAllConventionCustomers" Function="WebStack.QA.Test.OData.UnboundOperation.GetAllConventionCustomers" EntitySet="ConventionCustomers" IncludeInServiceDocument="true" />
Workaround: วิธีแก้ปัญหาสำหรับหัวข้อนี้คือการ add ทั้งสอง function คือ overloads FunctionImports.
OData Routing
ตัวอักษร String ที่อยู่ใน URL encoded slash (%2F), และ backslash(%5C) เป็นสาเหตุให้เกิด error 404 เมื่อถูกใช้ใน paths ของ OData resource .
จากตัวอย่าง , ตัวอักษร string สามารถใช้ใน paths ของ OData resource เช่นเดียวกับ parameters ของ functions หรือ key values ของ entity sets.
/Employees/Total.GetCount(Name='Name%2F')
/Employees('Name%5C')
เมื่อ services รับ requests ตัว hosts จะ un-escape ตัว escape sequences ก่อน passing มันให้ Web API runtime. เพื่อป้องกันการโจมตีแบบด้านล่าง:
http://www.contoso.com/..%2F..%2F/Windows/System32/cmd.exe?/c+dir+c:
http://www.contoso.com/..%2F..%2F/Windows/System32/cmd.exe?/c+dir+c:
นี้ทำให้เกิด Web API OData stack เพื่อ return ค่า error 404 (Not Found). เพื่อป้องกัน error นี้, ตัวผู้ใช้ควรจะใจ้ double escape ตามลำดับจาก slash (%252F) และ backslash (%255C). มันจะไม่เกิดขึ้นจาก query strings อย่างเช่น /Employees?$filter=Name eq 'Name%2F'
ข้อควรจำ un-escaped slashes ('/') และ backslashes ('\') นั้นไม่สามารถใช้ใน OData resource path ที่เป็น string literals ได้. Slashes ควรจะใช้ในการคั่น path และ backslashes ไม่ควรปรากฏใน OData resource path ทั้งหมด. (ทั้งคู่สามารถใช้ในส่วนของ OData query string.)
Workaround: คุณควร override ตัว Parse method ของ DefaultODataPathHandler เป็น escape อย่าง slash และ backslash ใน ตัวอักษร string ก่อนที่จะ parsing มันแบบปกติ. คุณสามารถหาวิธีที่ง่ายที่ใช้งานที่นี้.
Attribute Routing
Issue: Model binding ของ complex type มันเป็นการตกแต่ง FromUri attribute พฤติกรรมเมื่อใช้ Attribute Routing.
ตาม link นี้เป็นการติดตามหัวข้อและมีรายละเอียดเกี่ยวกับการแก้ปัญหา.
http://aspnetwebstack.codeplex.com/workitem/1944
http://aspnetwebstack.codeplex.com/workitem/1944
Issue: Scaffolding MVC/Web API ภายใน project กับ 5.2.0 packages แต่ใน 5.1.2 packages จะไม่มีอยู่ใน project.
การอัพเดท NuGet packages จาก ASP.NET MVC 5.2 ไม่ต้อง update ตัว Visual Studio tools เช่นเดียวกับ ASP.NET scaffolding หรือ ASP.NET Web Application project template. เราใช้ version ก่อนหน้าของ ASP.NET runtime packages (อย่างเช่น 5.1.2 ใน Update 2). ที่ทรัพยากรของ, ASP.NET scaffolding จะติดตั้งจาก version ก่อนหน้า (เช่น 5.1.2 in Update 2) จาก required packages, ถ้าพวกเราไม่มี projects อยู่แล้วจะต้องทำอย่างไร. ไม่ว่า, ASP.NET scaffolding ใน Visual Studio 2013 RTM หรือ Update 1 จะไม่ทำการ overwrite ตัว packages สุดท้ายที่คุณติดตั้งใน projects. ถ้าคุณใช้ scaffolding หลังจากอัพเดท packages จากตัว projects เป็น Web API 2.2 หรือ ASP.NET MVC 5.2, เพื่อให้แน่ใจว่า versions ของ Web API และ ASP.NET MVC นั้นถูกต้อง.
Bug Fixes และ Minor Feature Updates
การอัพเดทนี้มีการแก้ไข but และ มีกับเปลี่ยนแปลงบางสวนของความสามารถ. คุณสามารถหาข้อมูลที่สมบูรณ์ได้ตามลิ้งด้านล่าง:
No comments:
Post a Comment