asp.net web stack
DESCRIPTION
What new with ASP.NET 4.5 and ASP.NET MVC 4, Web API and SignalRTRANSCRIPT
![Page 1: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/1.jpg)
VS2012 for Web DevelopersUgo LattanziMicrosoft MVP ASP.NET/IIS, MCP
Twitter: @imperugoBlog: http://tostring.itE-mail: [email protected]
![Page 2: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/2.jpg)
Agenda
• ASP.NET MVC/Webform• Web API• SignalR
![Page 3: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/3.jpg)
WebStack
![Page 4: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/4.jpg)
ASP.NET MVC 4What’s new!
![Page 5: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/5.jpg)
New Release mode!
MVC 4, Web API, SignalR and so on are distribute by NuGet so you can update the packages directly into Visual Studio without download and install anything on your computer.
![Page 6: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/6.jpg)
New Template
• HTML5 Template (semantic tags, media queries, etc);
• It’s responsive (PC, Tablet and smartphone landscape/portrait)
![Page 7: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/7.jpg)
Async everywhere
![Page 8: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/8.jpg)
Asynchronous operations• The async is available also for ASP.NET
• MVC/Webform/WebAPI supports async/await keywords for long running action (I/O, Web Requests and so on);
![Page 9: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/9.jpg)
Asyn Controller
![Page 10: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/10.jpg)
Bundle Minification
![Page 11: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/11.jpg)
Bundle minification
Bundling and minification are two techniques you can use in ASP.NET 4.5 to improve request load time. Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)
![Page 12: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/12.jpg)
Bundle minification
Using B/M Without B/M Change
File Requests 9 34 256%
KB Sent 3.26 11.92 266%
KB Received 388.51 530 36%
Load Time 510 ms 780 ms 53%
![Page 13: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/13.jpg)
• Debug mode;• Reduce the number of requests;• Reduce the file size;• Custom Bundles;• CDN Support;• Directory Support;• LESS, CoffeeScript, SCSS, Sass Bundling
Bundle minification
![Page 14: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/14.jpg)
Debug bundle
![Page 15: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/15.jpg)
Create new js bundle
using System.Web.Optimization;
bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include("~/Scripts/jquery-ui-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include("~/Scripts/jquery.unobtrusive*","~/Scripts/jquery.validate*"));
![Page 16: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/16.jpg)
Create new css bundle
using System.Web.Optimization;
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include("~/Content/themes/base/jquery.ui.core.css","~/Content/themes/base/jquery.ui.resizable.css","~/Content/themes/base/jquery.ui.selectable.css","~/Content/themes/base/jquery.ui.accordion.css","~/Content/themes/base/jquery.ui.autocomplete.css","~/Content/themes/base/jquery.ui.button.css","~/Content/themes/base/jquery.ui.theme.css"));
![Page 17: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/17.jpg)
Custom Transformation
using System.Web.Optimization;
public class LessTransform : IBundleTransform{ public void Process(BundleContext context, BundleResponse response) { response.Content = dotless.Core.Less.Parse(response.Content); response.ContentType = "text/css"; }}
![Page 18: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/18.jpg)
Mobile Support
![Page 19: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/19.jpg)
Mobile support
• Mobile Template;• Device auto detect (mobile/desktop);• Support to mobile views (.mobile.cshtml);• Manual Check (Request.Browser.IsMobileDevice);• Override
HttpContext.SetOverriddenBrowser(BrowserOverride.Desktop);
![Page 20: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/20.jpg)
Mobile support
DisplayModeProvider.Instance.Modes.Insert(0, newDefaultDisplayMode("iPhone"){ ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf ("iPhone", StringComparison.OrdinalIgnoreCase) >= 0) });
![Page 21: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/21.jpg)
Social Network
![Page 22: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/22.jpg)
Social Network
• Enabling Logins from social networks (Facebook, Twitter, Google, LinkedIn, Live, Yahoo)
• Retrieve data after social login;• Easy to setup (just add your api key/secret into AuthConfig.cs)• Using DotNetOpenOAuth;
![Page 23: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/23.jpg)
More and more
![Page 24: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/24.jpg)
More stuff
• JSON.NET• Empty Project Template;• Azure SDK (1.6);• Database Migrations;• Add Controller to any project folder
![Page 25: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/25.jpg)
Demo
![Page 26: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/26.jpg)
Questions?
![Page 27: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/27.jpg)
Web API
![Page 28: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/28.jpg)
Web API and REST
When you speak about Web API, probably you should know REST, but was doesn it mean?
REST= REpresentational State Transfer
![Page 29: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/29.jpg)
Web API
It is not a WebService, a patter or a protocol, but is a style of software architecture for
distributed systems such as the World Wide Web
![Page 30: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/30.jpg)
Web API
ASP.NET Web API is a framework (FW 4.0) for processing data and returning data, tipically in json or xml (RESTful services);
It seems MVC but is not and, if you need both, use both.
![Page 31: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/31.jpg)
What is similar to MVC?
• Released with NuGet;• Routing;• Controllers and Actions;• Filters;• ModelBindings;• Dependency Injection;
![Page 32: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/32.jpg)
What is different from MVC?
• Dispatching (based on http verbs);• Formatters;• Async everywhere;• Self host (no need IIS);• Content negotiation;
• Everything is under System.Web.Http;
![Page 33: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/33.jpg)
How does it work?public class ValuesController : ApiController{// GET api/valuespublic IEnumerable<string> Get(){return new string[] { "value1", "value2" };}
// GET api/values/5public string Get(int id){return "value";}}
// POST api/valuespublic void Post([FromBody]string value){}
// PUT api/values/5public void Put(int id, [FromBody]string value){}
// DELETE api/values/5public void Delete(int id){}}
![Page 34: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/34.jpg)
Conventions
If you don’t like the conventions, don’t worry you can override using the attributes [HttpGet] and [NoAction]
![Page 35: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/35.jpg)
Client
Into System.Web.Http (assembly Microsoft.AspNet.WebApi.Client) there is also a client.
HttpClient client = new HttpClient();var requestUrl = new Uri("http://api.mysite.com/something");var response = client.GetStringAsync(requestUrl);
![Page 36: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/36.jpg)
Errors handling
• By default the response in 500;• You can manage differently in a single response, or writing a
different default behavior.
![Page 37: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/37.jpg)
Errors handling• By default the response in 500;• You can manage differently in a single response, or writing a
different default behavior.
public string Get(int id){ if(id<1) throw new HttpResponseException(HttpStatusCode.NotFound);
//DO SOMETHING return "value";}
![Page 38: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/38.jpg)
Content Negotiation
• The response format depends from the client;• You can add custom formatters for the response, out of the box
json and xml are supported;
![Page 39: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/39.jpg)
Demo
![Page 40: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/40.jpg)
Questions?
![Page 41: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/41.jpg)
SignalR
![Page 42: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/42.jpg)
SignalR
Async signaling library for .NET to help build real-time, multi-user interactive web applications ( aka persistent connection abstraction for .NET);
Install-Package SignalR;Install-Package SignalR.SampleMIT License
![Page 43: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/43.jpg)
SignalR
• Web Sockets (several fallback like «forever frame» and «Long polling»);
• Different clients (JS, Silverlight, Window8, WPF, Windows Phone);
![Page 44: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/44.jpg)
super-Demo
![Page 45: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/45.jpg)
Questions?
![Page 46: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/46.jpg)
THANKS
![Page 47: ASP.NET Web Stack](https://reader035.vdocuments.us/reader035/viewer/2022081413/548adb3bb47959b4608b4580/html5/thumbnails/47.jpg)
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.