Building Web Applications with Microsoft ASP

Slides:



Advertisements
Similar presentations
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Advertisements

INTRODUCTION TO ASP.NET MVC AND EXAMPLE WALKTHROUGH RAJAT ARYA EFECS - OIM DAWG – 4/21/2009 ASP.NET MVC.
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
Building Modern Websites with ASP.NET Rachel Appel
06 | Implementing Web APIs Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek.
Virtual techdays INDIA │ November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.
1 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and Custom Forms.
JavaScript & jQuery the missing manual Chapter 11
ASP.NET Web API Udaiappa Ramachandran NHDN-Nashua.NET/Cloud Computing UG Lead Blog:
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
Introduction to Entity Framework Part 2 CRUD Scaffolding Tom Perkins NTPCUG.
Starting with our inputs, we will add some constraints to enable client-side validation Kuali University: Apply Now Lab 2: Validation and Constraints Lab.
Introduction to MVC Controllers NTPCUG Tom Perkins, Ph.D.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
06 | HTTP Services with Web API Bruno Terkaly | Technical Evangelist Bret Stateham | Technical Evangelist.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Virtual techdays INDIA │ 9-11 February 2011 SESSION TITLE Kamala Rajan S │ Technical Manager, Marlabs.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
BIT 286: Web Applications ASP.Net MVC. Objectives Applied MVC overview Controllers Intro to Routing Views ‘Convention over configuration’ Layout files.
Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Part VI: Mapping URLs.
1 Using MVC 6. MVC vs. ASP Web Forms Both run under ASP.NET Can coexist In Web Forms, browser requests page. xxx.aspx and xxx.aspx.cs on the server Page.
Introduction to MVC Slavomír Moroz. Revision from Previous Lesson o ASP.NET WebForms applications Abstract away HTTP (similar to desktop app development)
Data and tools on the Web have been exposed in a RESTful manner. Taverna provides a custom processor for accessing such services.
Build Data Driven Apps with ASP.NET Core Rachel Appel.
Jim Fawcett CSE686 – Internet Programming Spring 2014
Building Web Applications with Microsoft ASP
Building Web Applications with Microsoft ASP
ASP.NET Programming with C# and SQL Server First Edition
ASP.NET Forms.
Introduction to .NET Florin Olariu
An introduction to ASP.Net with MVC Nischal S
Building production ready APIs with ASP.NET Core 2.0
Building Web Applications with Microsoft ASP
API (Application Program Interface)
Jim Fawcett CSE686 – Internet Programming Spring 2012
Introduction to Classes and Objects
Node.js Express Web Applications
C#: ASP.NET MVC Overview
Play Framework: Introduction
Node.js Express Web Services
Pre assessment Questions
J2EE Lecture 7: Spring – Spring MVC
Building Web Applications with Microsoft ASP
Building Web Applications with Microsoft ASP
04 | Customizing Controllers
02 | Developing ASP.NET MVC 4 Models
Testing REST IPA using POSTMAN
Microsoft Build /11/2018 2:12 AM © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY,
DotnetConf 11/17/ :06 AM © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE.
Chapter 3 Introduction to Classes, Objects Methods and Strings
MVC6. Awesomeness is here!
REST Client Tutorial by Pavan Ethic Coder
Data Structures and Database Applications Custom Models with MVC
Introduction to .net Impersonation
Controllers.
Authorization in Asp.Net Core
Social Media And Global Computing Managing MVC with Custom Models
Building HTTP services for modern client apps
Web Development Using ASP .NET
IT College 2016, Andres käver
Building production-ready APIs with ASP.NET Core 2.2
Building Web Applications with Microsoft ASP
ASP.NET MVC Web Development
#01# ASP.NET Core Overview Design by: TEDU Trainer: Bach Ngoc Toan
Chengyu Sun California State University, Los Angeles
Security - Forms Authentication
Concepts in ASP.NET Core App
Chengyu Sun California State University, Los Angeles
ASP.NET Core Middleware Fundamentals
Presentation transcript:

Building Web Applications with Microsoft ASP Building Web Applications with Microsoft ASP.NET - I376 Web Application Programming II – I713 IT College, Andres Käver, 2016-2017, Spring semester Web: http://enos.Itcollege.ee/~akaver/ASP.NETCore Skype: akaver Email: akaver@itcollege.ee

MVC Model View Controller ViewModel Business logic and its persistence Template for displaying data Controller Communication between Model, View and end-user ViewModel Models used for supplying views with complex (strongly typed) data

MVC – Request Pipeline

MVC – Simplest ASP.NET Core app using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; public class Startup { public void Configure(IApplicationBuilder app) app.Run(async context => await context.Response.WriteAsync("Hello, World!"); }); }

MVC – Middleware ordering public class Startup { public void Configure(IApplicationBuilder app) app.Use(async (context, next) => // Do work that doesn't write to the Response. await next.Invoke(); // Do logging or other work that doesn't write to the Response. }); app.Run(async context => await context.Response.WriteAsync("Hello from 2nd delegate."); }

MVC – Middleware ordering Middleware is called in order it is added public void Configure(IApplicationBuilder app) { app.UseExceptionHandler("/Home/Error"); // Call first to catch exceptions // thrown in the following middleware. app.UseStaticFiles(); // Return static files and end pipeline. app.UseIdentity(); // Authenticate before you access // secure resources. app.UseMvc(); // Add MVC to the request pipeline. }

MVC - Middleware Pipeline is configured with methods Run – short circuit, return Map - branch Use – Chaining middleware-s together

MVC - Controller Controller – define and group actions (or action method) for servicing incoming web requests. Controller can be any class that ends in “Controller” or inherits from class that ends with “Controller” Convention is (but are not required) Controllers are located in “Controllers” folder Controllers inherit from Microsoft.AspNetCore.Mvc.Controller The Controller is a UI level abstraction. Its responsibility is to ensure incoming request data is valid and to choose which view or result should be returned. In well-factored apps it will not directly include data access or business logic, but instead will delegate to services handling these responsibilities.

MVC - Controller Inheriting from base Controller gives lots of helpful methods and properties Most importantly returning various responses View Return View(viewModel); HTTP Status Code Return BadRequest(); Formatted Response Return Json(someObject); Content negotiated response Return Ok(); Redirect Return RedirectToAction(“Complete”, viewModel);

MVC - Routing Routing middleware is used to match incoming requests to suitable controller and action Actions are routed by convention or by attribute Routing middleware (sets up the convention) By ”classic” convention, request should take this form …../SomeController/SomeAction/ID app.UseMvc(configureRoutes: routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });

MVC – Multiple routes Routes are matched from top-down app.UseMvc(routes => { routes.MapRoute(name: "article", template: "/article/{name}/{id}", defaults: new { controller = "Blog", action = "Article"}); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); Routes are matched from top-down On first match corresponding controller.action is called

MVC – Routes – mutiple matching actions When multiple actions match, MVC must select suitable action Use HTTP verbs to select correct action If not possible to choose, exception is thrown show form -> submit form pattern is common public class ProductsController : Controller { public IActionResult Edit(int id) { ... } [HttpPost] public IActionResult Edit(int id, Product product) { ... } }

MVC – Routing - Attributes public class MyDemoController : Controller { [Route("")] [Route("Home")] [Route("Home/Index")] public IActionResult MyIndex() return View("Index"); } [Route("Home/About")] public IActionResult MyAbout() return View("About"); [Route("Home/Contact")] public IActionResult MyContact() return View("Contact"); With attribute routing, controller and action name have no role! Typically attribute based routing is used in api programming

MVC – Routing - Attributes [HttpGet("/products")] public IActionResult ListProducts() { // ... } [HttpPost("/products")] public IActionResult CreateProduct(...) } HttpGet("/products/{id}"] public IActionResult GetProduct(int id) { // ... } Routing can also make use of the Http[Verb] attributes such as HttpPostAttribute

MVC - Combining routes Route attributes on the controller are combined with route attributes on the individual actions. Placing a route attribute on the controller makes all actions in the controller use attribute routing. [Route("products")] public class ProductsApiController : Controller { [HttpGet] public IActionResult ListProducts() { ... } [HttpGet("{id}")] public ActionResult GetProduct(int id) { ... } }

MVC - Combining routes Route templates applied to an action that begin with a / do not get combined with route templates applied to the controller. [Route("Home")] public class HomeController : Controller { [Route("")] // Combines to define the route template "Home" [Route("Index")] // Combines to define the route template "Home/Index" [Route("/")] // Does not combine, defines the route template "" public IActionResult Index() {...} [Route("About")] // Combines to define the route template "Home/About" public IActionResult About() }

MVC - Token replacement in route templates Attribute routes support token replacement by enclosing a token in square-braces ([ ]). The tokens [action], [area], and [controller] will be replaced with the values of the action name, area name, and controller name from the action where the route is defined. [Route("[controller]/[action]")] public class ProductsController : Controller { [HttpGet] // Matches '/Products/List' public IActionResult List() {... } [HttpGet("{id}")] // Matches '/Products/Edit/{id}' public IActionResult Edit(int id) {…} }

MVC – Route template { } – define route parameters Can specify multiple, must be separated by literal value Must have a name, my have additional attributes * - catch all parameter blog/{*foo} would match any URI that started with /blog and had any value following it (which would be assigned to the foo route value).  Route parameters may have default values {controller=Home} name? – may be optional name:int – use : to specify an route constraint blog/{article:minlength(10)}

MVC – Route constraints Avoid using constraints for input validation, because doing so means that invalid input will result in a 404 (Not Found) instead of a 400 with an appropriate error message. Route constraints should be used to disambiguate between similar routes, not to validate the inputs for a particular route. Constraints are int, bool, datetime, decimal, double, float, guid, long minlength(value), maxlength(value), length(value), length(min,max) min(value), max(value), range(min, max) alpha, regex(expression), required

MVC – Model binding Maps data from from HTTP request to action parameters Order of binding Form values (POST) Route values Query string (…foo/?bar=fido) Route: {controller=Home}/{action=Index}/{id?} request: …./movies/edit/2 public IActionResult Edit(int? id) Model binding also works on complex types – reflection, recursion – type must have default constructor

MVC – Model binding attributes BindRequired – adds and model state error, if cannot bind BindNever – switch off binder for this parameter FromHeader, FromQuery, FromRoute, FromForm – select source FromBody – from request body, use formatter based on content type (json, xml)

MVC – Model validation Validation attributes mostly in System.ComponentModel.DataAnnotations [Required], [MaxLength()], etc Model validation occurs prior to action invokation Actions has to inspect ModelState.IsValid If needed, call TryValidateModel(someModel) again Custom validation, client-side validation, remote validation - later