ict.ken.be

 

Posts in Category: Notes

MVC4 Fundamentals and the WebApi - Notes 

Categories: Notes WebApi
MVC4 Fundamentals and the WebApi
Scott Allen

I only looked at this module of the course. John Papa referred to it in his single page application course. Not much new except if you didn't know about curl and handlebars.

  • System.Web.Http
  • System.Web.Http.WebHost
  • GET, POST, PUT, DELETE by default
  • [AcceptVerbs] for others

Scott preferes using a plural name. Eg. VideosController

cUrl
  • curl http://somewebsite/api/videos
  • curl http://somewebsite/api/videos -X GET
  • curl http://somewebsite/api/videos -X DELETE

-> File not found exception, then IIS needs to be configured

-> IISExpress applicationhost file add on ExtentionlessUrl the verbs

  • curl http://somewebsite/api/videos -X GET -v
  • curl http://somewebsite/api/videos -X GET -H "Accept: application/json"
  • curl http://somewebsite/api/videos -X POST -d "value=scott"
  • curl http://somewebsite/api/videos -X POST -d "=scott"
Content negotiation
  • Same resource can have multiple representations.
  • Accept: image/png
  • Accept: Text/html 
Action Parameters
  • Primitive types assumed to not be in the message body, unless public void Post([FromBody]string value) {}
  • Complex types assumed in the message body
  • Only a  single model allowed from the message body
EF NuGet 
  • enable-migrations
  • Seed(... AddOrUpdate( ) )
  • update-database
  • db.Configuration.ProxyCreationEnabled = false;
Showing Data in view
  • Update-Package jQuery
  • Update-Package HandleBars
  • <script> type attribute is no longer needed in Html5

var videoApiUrl = '@Url.RouteUrl("DefaultApi", new { httproute="" , ... '

//global error handler

$(document).ajaxError(function (event, xhr) {

alert(xhr.status + ":" + xhr.statusText);

}

<script id="template" type="text/html>

{{#each video}}

{{Title}}

{{/each}}

</script>

Handlebars.compile($("#videoTable").html());

public Video GetVideo(int id)

{

   var video =  db.Find(id); 

   if (video==null)

   {

     throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));

   }

   return video;

}

public HttpResponseMessage PutVideo(int id, Video video)

{

   db.Entry(video).State = EntityState.Modified;

.. HttpStatus NotFound OK BadRequest

}

On post set 201 Created and add response.Headers.Location

On Delete check for DbUpdateConcurrencyException

install-package Microsoft.AspNet.WebApi.Client

var client = new HttpClient();

client.DefaultRequestHeaders.Accept.Add(

   new MediaTypeWithQualityHeaderValue("application/xml"));

var result = client.GetAsync(new Uri(...))

 

TCP/IP Networking for Developers - Notes 

Categories: Network Notes
TCP/IP Networking for Developers
Steve Evans - http://sevans.info
This was by far the worst Pluralsight tutorial I have ever seen, I am not an expert in networking and had hoped to learn a thing or two. Unfortunately, the course was not only very unstructured but also not very enlighting. Not even for a computer noob.
 
  • ipconfig
  • ipconfig /all | more
  • ipconfig /displaydns

  • c:\windows\system32\drivers\etc\hosts
  • You can put multiple hostnames on one line
  • 127.0.0.1 ken.be patrycja.pl test.be

  • No DHCP then 169.254.x.x

 

  • Change your ip logging to ipV6
 
Name resolution
  • nslookup ken.be
  • nslookup + enter, server 8.8.4.4
 
  • a-record transforms hostname into ip
  • set type=NS (nameserver)
  • set type=MX (mailexchange)
  • set type=CN (canonicalname or alias)
  • set type=AAAA (quad a) returns ipv6
  • wildcards records
 
A router connects different subnets
  • tracert
  • pathping
 

subnets

  • 255.255.255.node
  • 255.255.node
  • 255.255.255.240
 

routes

  • 0.0.0.0 means any ip address (netmask 0.0.0.0 to gateway)
  • 127.anything is always the localhost (netmask 255.0.0.0)
  • gateway.0 all that are on-link
  • 255.255.255.255 broadcast that doesn't cross router
  • route print
 
NAT (Network Address Translation)
  • Private Network Ranges
  • 10.0.0.0/255.0.0.0
  • 172.16.0.0/255.240.0.0
  • 192.168.0.0/255.255.0.0
 
Port Connectivity
  • TCP (Transmision Control Protocol) - request missing
  • UDP (User Datagram Protocol) - no check if received - no session
 
  • telnet ken.be 80
  • 400 Bad Request
  • port 1433 standard mssql
  • port 25 smtp
 
  • nmap -v servername (zenmap is win gui)
  • netstat -ano
 
Windows firewall
  • Log dropped packets
 
ICMPv4 protocol used by ping
Network Capture
Wireshark : right-click and follow tcp stream
Fiddler
 

 

Introduction to the ASP.Net WebApi - Notes 

Categories: Notes WebApi

Introduction to the ASP.NET WebApi
Jon Flanders

  • Service layer for HTML5, mobile applications, ...
  • REST : Representational State Transfer (Roy Fielding)
  • Full rest -> Level4 service
RouteTable.Routes.MapHttpRoute(
"InstructorRoute",
"{controller}/{id},
new { id = RouteParameter.Optional }
);
 
ApiController
[Authorize(users="jon")]
public string Get() //returns all
{
  return this.Request.GetUserPrinciple().Identity.Name;
}
public HttpResponseMessage<Instructor> Get(int id)
{
   var instructor = (from ...)
   if (null == instructor)
   {
      //return 404
      var notfoundMessage = new HttpRequestMessage<Instructor>(HttpStatusCode.NotFound);
      return notfoundMessage;       
   }
   var newMessage = new HttpRequestMessage<Instructor>(instructor);
   return newMessage;
}
public HttpResponseMessage<Instructor> Post(Instructor instructor)
   //create and insert and return 201 
   var newMessage = new HttpRequestMessage<Instructor>(instructor, HttpStatusCode.created);
   newMessage.Headers.Location = 
      new Uri(Request.RequestUri, "/instructors/" + instructor.ID.ToString());
}
public Instructor Put(Instructor instructor)
{ update }
public Instructor Delete(int id)
{ remove }
System.Net.Http
System.Net.Http.Formatting
System.Web.Http
System.Web.Http.Common
System.Web.Http.WebHost
System.Web.Http.SelfHost
System.Web.Http.Data
System.Web.Http.Data.EntityFramework
System.Web.Http.Data.Helpers 
  • ModelBinder for Body, header, querystring
  • MediaTypeFormatters
  • Validation via attributes
  • HttpRequestMessage and HttpResponseMessage
  • Content negotiation (xml or json)
  • Customize file extension or querystring
  • Fidler : Post with Content-Type:application/json
  • http://odata.org with IQueryable
  • HttpConfiguration class
  • GlobalConfiguration.Configuration
  • DependencyResolver 
Uniform interface of REST
  • Start with URI, add well-known HTTP verbs
  • Get: cacheble, safe, always same effect (idempotent)
  • Post: create new resource, unsafe, non-cached 
  • Put: update, idempotent
  • Delete: remove, idempotent
  • You can specify multiple verbs to one method
ApiController: IHttpController, IDisposable
{
   Configuration, ControllerContext, ModelState, Request, Url, Task<HttpResponseMessage>, ExecuteAsync, Initialize
}
 
HttpRequestMessage:IDisposable
{
  Content, Headers, Method, Properties, RequestUri, Version, Displose
}
 
HttpResponseMessage:IDisposable
{
  Content, Headers, IsSuccessStatusCode, ReasonPhrase, RequestMesage, StatusCode, Version, Dispose, EnsureSuccessStatusCode
}
$.ajax({
url: '/api/foo/id',
success: function (data) {},
statusCode: { 
404: function() {} 
}
}); 
HttpClient
  • HttpWebRequest, WebClient
var c = new HttpClient();
c.GetAsync(uri).ContinueWith((t) =>
{
  HttpResponseMessage response = null;
  response = t.Result;
  response.EnsureSuccessStatusCode();
  response.Content.ReadAsAsync<JsonObject>().ContinueWith(readTask) =>
{
  ProcessJson(readTask.Result);
}
});
  • http://search.twitter.com/search.json?q=fun
  • PostAsync, PutAsync
  • ObjectContent<T> for Json or XML
  • StreamContent for raw data
  • MultipartContent and MultiPartFormContent
  • ByteArrayContent, FormUrlEncodedContent, StringContent
  • UploadVideoClient (HttpClient)
  • Dealing with headers
  • Dealing with cookies 
Per-request headers
var pcontent = new HttpRequestMessage<Person>().CreateContent<Person>(new Person());
pcontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
pcontent.Headers.Add("Accept","application/xml");
  • public class HttpClientHandler : HttpMessageHandler
  • Derive from DelegatingHandler
  • The HttpClient does not send a UserAgent by default
  • client.DefaultRequestHeaders.Add("User-Agent","Jon's Cool Client");
  • Token-based authorization can be accomplished by a custom HttpClientHandler
Self-Hosting Example
  • Create a HttpConfiguration (eg. HttpSelfHostConfiguration)
  • Create your HttpServer (eg. HttpSelfHostServer)
  • Start listening: HttpServer.OpenAsync();
  • When using the first contructor overload, you end up with HttpControllerDispatcher as your HttpMessageHandler. It allows you to use the same ASP.NET routing and same convention or configuration settings as ApiController in ASP.Net Hosting.
  • config.Routes.MapHttpRoute("DefaultRoute", "{controller}/{id}", new { id=Parameter.Optional });
  • You can pass HttpServer to HttpClient, useful for self-hosting testing (all in memory)
var client = new HttpClient(server);
client.GetAsync(...).ContinueWith(
(t)=>{ var result = t.Result; result.Content.ReadAsStringAsync().ContinueWith(
(tr) => { Console.Write(rt.Result);})
;});
Security
ASP.NET Authentication or WCF for self-hosting
 
AllowAnonymousAttribute : explicitly allow access to action of apicontroller
 
Forms authentication
  • /account/jsonlogin endpoint with MVC4 projects
  • Authentication_JSON_AppService.axd
  • App gets cookie
 
Basic authentication
 
  • HttpClient/WebHttpRequest/WebClient understand Windows authentication
 
You can add support for token authentication (eg OAuth) fairly easily
 
Authorization
  • AuthorizeAttribute with names or roles on individual actions (returns 401 if unauthorized)
Self-host
  • HttpMessageHandler
  • request.GetUserPrincipal().Identity.Name;
  • HttpSelfHostConfiguration
protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
{
   httpBinding.Security.Mode = HttpBindingSecurity.TransportCredentials;
   httpBinding.Security.Transport.CredentialType=HttpClientCredentialType.Windows;
   return base.OnConfigureBinding(httpBinding);
Asp.Net Hosting
  • Global.asax.cs
  • GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
Extensibility
  • GlobalConfiguration.Configuration
  • HttpConfiguration
  • Filters, Formatters, MessageHandlers, Routes, ServiceResolver
  • IncludeErrorDetailPolicy.Always / Never / LocalOnly
 
Formatters will match incoming Content-Type with a MediaTypeFormatter (content negotiation and serialization)
  • JsonP
  • Inject code that isn't in same domain as controller
  • HttpResponseMessage<JsonPReturn> Get(int id, string callback)
  • response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/javascript");
<script>
function myCallback(obj) { document.getElementById('msg').innerText = obj.Data; }
</script>
<script src="http://server.com/api/controller/values/1?callback=myCallBack" />
Filters hook into the model binding and controller execution pipeline (model-related issues)
  • AuthorizeAttribute is an IFilter
  • MyFilter : IActionFilter
  • actionContext.ActionArguments 
 
Message Handlers are the http processing pipeline base class (http-issues)
  • HttpServer, HttpClientHandler, HttpControllerDispatcher
public class MyMethodOverrideHandler : DelegatingHandler
{
   const string header = "X-HTTP-Method-Override";
   //override SendAsync 
   //and let clients put the real verb in this header
}
ServiceResolver (internal implementation detail extensibility)
  • Implement IDependencyResolver or Call SetResolver and pass in two delegates
Action Filter vs. Mesage Handler
  • MessageHandler - before model binding - HTTP message level - eg. run an xml Schema validation
  • ActionFilter - after model binding - Extending your controller - eg. run an object validation

 

Page 5 of 5 << < 1 2 3 4 5