0% found this document useful (0 votes)
136 views38 pages

Adding WebApi To ASPNET Site

The document compares and contrasts WCF and ASP.NET Web API. WCF is for back-end services using SOAP and various transports, while ASP.NET Web API is for front-end services using HTTP and REST. The document also provides code examples for building a basic Web API with attribute routing and handling file uploads.

Uploaded by

silly_rabbitz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
136 views38 pages

Adding WebApi To ASPNET Site

The document compares and contrasts WCF and ASP.NET Web API. WCF is for back-end services using SOAP and various transports, while ASP.NET Web API is for front-end services using HTTP and REST. The document also provides code examples for building a basic Web API with attribute routing and handling file uploads.

Uploaded by

silly_rabbitz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 38

Microsoft /web

Microsoft /web

WCF Back-end Services SOAP, WS-* Transports: HTTP, TCP, UDP, Queues, WebSockets, custom Message patterns: requestreply, one-way, duplex Use WCF Web HTTP to add HTTP endpoints to existing WCF services Use WCF Data Services for full OData support Microsoft /web

ASP.NET Web API Front-end Services Media Types: JSON, XML, form-URL-encoded, custom HTTP only Request-reply only REST, resource-centric Use SignalR for asynchronous signaling (polling, longpolling, WebSockets)

Microsoft /web

Microsoft /web

Microsoft /web

Microsoft /web

Microsoft /web

Microsoft /web

Microsoft /web

Microsoft /web

A successful API call returns an HTTP OK and the JSON data

Microsoft /web

An unsuccessful API call returns an HTTP 404 (and no JSON)

Microsoft /web

Microsoft /web

Microsoft /web

Microsoft /web

Microsoft /web

Microsoft /web

public async Task<IList<string>> Post() { List<string> result = new List<string>(); if (Request.Content.IsMimeMultipartContent()) { MultipartFormDataStreamProvider stream = new MultipartFormDataStreamProvider("c:/uploads/"); IEnumerable<HttpContent> bodyparts = await Request.Content.ReadAsMultipartAsync(stream); IDictionary<string, string> bodyPartFiles = stream.BodyPartFileNames; bodyPartFiles .Select(i => { return i.Key; }) .ToList() .ForEach(x => result.Add(x)); } return result; }

Microsoft /web

Microsoft /web

Microsoft /web

Why Not?

Microsoft /web

Microsoft /web

class Program { static void Main(string[] args) { // configure the server var baseAddress = "http://localhost:8080/"; var config = new HttpSelfHostConfiguration(baseAddress); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // Create and open the server var server = new HttpSelfHostServer(config); server.OpenAsync().Wait(); Console.WriteLine("The server is running..."); Console.ReadLine(); } }

Microsoft /web

public class EnvironmentStatus { public string MachineName { get; set; } public DateTime TimeOnServer { get; set; } }

public class EnvironmentController : ApiController { public EnvironmentStatus Get() { Console.WriteLine(User agent " + Request.Headers.UserAgent);
return new EnvironmentStatus { MachineName = Environment.MachineName, TimeOnServer = DateTime.Now }; } }

Microsoft /web

Microsoft /web

Microsoft /web

Microsoft /web

http://forums.dev.windows.com http://bldw.in/SessionFeedback

Microsoft /web

You might also like