ASP.NET Web API O to 100

2,276 views
2,137 views

Published on

Presented at VIC.NET User group on 29/1/2013

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,276
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • http://www.strathweb.com/2012/12/running-aspnet-web-api-with-owin-and-katana/Open Web Interface for .NETKatana is an OWIN implementation for Microsoft hosts (HttpListener, System.Web) and the Web API framework.
  • 1. What it is?2. Basic template (Talk about layout and basic structure Routing/Verbs/Help pages, Default Formatters, Content Negotiation) 2a (Demo-Create a basic Web API as it is from box)3. Getting Started 3a (Demo-Modify the template which was created)
  • 1. What it is?2. Basic template (Talk about layout and basic structure Routing/Verbs/Help pages, Default Formatters, Content Negotiation) 2a (Demo-Create a basic Web API as it is from box)3. Getting Started 3a (Demo-Modify the template which was created)
  • Filters are used to handle cross cutting concernsFilter types:Action filters run before and after invoking an actionAuthorization filters run before model binding and are specifically for authorizing the userException filters handle generating responses for error cases in a centralized wayFilters can be configured globally, per controller, or per action as an attribute
  • Talk about Exception Filter. Create sample filter to the pipeline and show it
  • Simple types: Route data, query parametersComplex types Configured MediaTypeFormatters are used to deserialize the request body based the content type JSON, XML and form-url-encoded supported out of the box.
  • http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspxIf the parameter has no attribute on it, then the decision is made purely on the parameter’s .NET type.“Simple types” uses model binding. Complex types uses the formatters. A “simple type” includes:primitives, TimeSpan, DateTime, Guid, Decimal, String, or something with a TypeConverter that converts from strings.You can use a [FromBody] attribute to specify that a parameter should be from the body.You can use a [ModelBinder] attribute on the parameter or the parameter’s type to specify that a parameter should be model bound. This attribute also lets you configure the model binder.  [FromUri] is a derived instance of [ModelBinder] that specifically configures a model binder to only look in the URI.The body can only be read once.  So if you have 2 complex types in the signature, at least one of them must have a [ModelBinder] attribute on it.Differences with MVCHere are some differences between MVC and WebAPI’s parameter binding:MVC only had model binders and no formatters. That’s because MVC would model bind over the request’s body (which it commonly expected to just be FormUrl encoded), whereas WebAPI uses a serializer over the request’s body.MVC buffered the request body, and so could easily feed it into model binding. WebAPI does not buffer the request body, and so does not model bind against the request body by default.WebAPI’s binding can be determined entirely statically based off the action signature types. For example, in WebAPI, you know statically whether a parameter will bind against the body or the query string. Whereas in MVC, the model binding system would search both body and query string.
  • http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx
  • http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx
  • http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx
  • (QueryablevsODataResult<>) Demo OData (Queryable/ODataResult) (talk about what is supported:top,skip,filter and orderby)e.g demo three types of Querying, filtering and paging
  • Simple types: Route data, query parametersComplex types Configured MediaTypeFormatters are used to deserialize the request body based the content type JSON, XML and form-url-encoded supported out of the box.
  • e.g. BSON Formatter
  • e.g.ETagHandler
  • e.gBasicAuthenticationHandler
  • ASP.NET Web API O to 100

    1. 1. Discover. Master. Influence.
    2. 2. Senior Consultant at Readify @H_Desai http://himanshudesai.wordpress.com Discover. Master. Influence. 2
    3. 3. • Getting up-to speed• Going Deep• Other Concerns• Lessons from the field Discover. Master. Influence.
    4. 4. ASP.NET Web API Features• From ASP.NET MVC • From WCF Web API• Link generation • Server-side query composition• Testability • Create custom help pages• IoC integration • Self-host• VS template • Tracing• Scaffolding Discover. Master. Influence. 4
    5. 5. • Two hosting options today – ASP.NET Web Application(IIS) – Self-host server option (console application, windows service, Azure worker Role etc)• HttpConfiguration is the common denominator• Host in memory for end-to-end testing• Use an OWIN bridge to host on any OWIN compatible web server Discover. Master. Influence. 5
    6. 6. Discover. Master. Influence. 6
    7. 7. Discover. Master. Influence. 7
    8. 8. Discover. Master. Influence.
    9. 9. HTTP request/response handling Client Server
    10. 10. • Message Handlers Message Handler Message HttpServer Message Handler Http ApiController Handler Dispatcher Discover. Master. Influence. 10
    11. 11. • Http Dispatcher Invoke Action Action Filters Model Bind Authorization Filters Select action Exception filters Route to controller Formatting Request Response Discover. Master. Influence. 11
    12. 12. • Action Filters• Authorization Filters• Exception Filters Discover. Master. Influence. 12
    13. 13. Discover. Master. Influence. 13
    14. 14. • Simple types are taken from the URI• Complex types come from the body• Override using [FromUri],[FromBody],[ModelBinder], custom parameter binding Discover. Master. Influence. 14
    15. 15. • HttpRequest->.NET Type/?id=123&name=bobvoid Action(int id, string name)// both parameters are simple types and will come from url/?id=123&name=bobvoid Action([FromUri] int id, [FromUri] string name)// paranoid version of above. Discover. Master. Influence. 15
    16. 16. void Action([FromBody] string name);// explicitly read the body as a string./?id=123void Action(int id, Customer c)// id from query string, c is a complex object, comes frombody via a formatter. Discover. Master. Influence. 16
    17. 17. void Action(Customer c1, Customer c2)// error! multiple parameters attempting to read from thebodyvoid Action([FromUri] Customer c1, Customer c2) // ok, c1 isfrom the URI and c2 is from the body Discover. Master. Influence. 17
    18. 18. void Action([ModelBinder(MyCustomBinder)] SomeType c)// Specifies a precise model binder to use to create theparameter.[ModelBinder(MyCustomBinder)]public class SomeType { }// place attribute on type declaration to apply to allparameter instancesvoid Action(SomeType c) Discover. Master. Influence. 18
    19. 19. Discover. Master. Influence.
    20. 20. Discover. Master. Influence. 20
    21. 21. Discover. Master. Influence.
    22. 22. • Media Type Formatter• Delegating Handler• IOC• Services (e.g. Tracing) Discover. Master. Influence. 22
    23. 23. Discover. Master. Influence.
    24. 24. • Built-In – Json – Xml – FormUrl-Encoded – ODataMediaType• Custom Discover. Master. Influence. 24
    25. 25. Discover. Master. Influence. 25
    26. 26. Discover. Master. Influence.
    27. 27. Discover. Master. Influence. 27
    28. 28. Discover. Master. Influence.
    29. 29. Discover. Master. Influence. 29
    30. 30. Discover. Master. Influence.
    31. 31. Discover. Master. Influence. 33
    32. 32. Discover. Master. Influence.
    33. 33. • Demo – Test Controller via Http – Test Delegating Handlers Discover. Master. Influence. 35
    34. 34. • Be careful about OData Versioning (alpha right?)• Message Handler error handling• Don’t let your EF code leak out.• Ensure you write HTTP level tests for controller.• Tracing is enabled.• Remove Formatters you don’t need.• Add JSONP and CORS formatters (Not available by default) Discover. Master. Influence. 36
    35. 35. Discover. Master. Influence.

    ×