Upload
girishkumar-kaisetty
View
224
Download
0
Embed Size (px)
Citation preview
7/27/2019 ASP.net Mvc Part 1
1/36
ASP.NET MVC Part I
Ventsislav PopovDeveloper Evangelist at Microsoft
ventsypopov.comventsypopov.com
7/27/2019 ASP.net Mvc Part 1
2/36
Agenda Beforehand ASP.NET Web Forms
What is MVC What is ASP.NET MVC?
Models
Views
Controllers
Validation
Routing
Unit Tests
View engines
2ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
3/36
ASP.NET Web Forms
Rich controls and tools
Postbacks
Event driven web development
Viewstate Less control over the HTML
Hard to test
Rapid development
ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
4/36
Lets chat for a bit
4ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
5/36
7/27/2019 ASP.net Mvc Part 1
6/36
Model View - Controller
6
Controller - responsible for handling all userinput
Model - represents the logic of the application
View - the visual representation of the model
ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
7/36
ASP.NET MVC
More control over HTML
No Codebehind
Separation of concerns
Easy to test URL routing
No postbacks
No ViewState
7ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
8/36
Models
The model should contain all of the applicationbusiness logic, validation logic, and databaseaccess logic.
ASP.NET MVC is compatible with any dataaccess technology (for example LINQ to SQL)
All .edmx files, .dbml files etc. are located inthe Models folder.
8ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
9/36
Custom View Models
When you combine properties to display on aView
namespace ContosoUniversity.ViewModels{
public class AssignedCourseData{
public int CourseID { get; set; }public string Title { get; set; }public bool Assigned { get; set; }
}}
ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
10/36
Creating a Model - DEMO
10ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
11/36
What is Controller? It is a class
Derives from the baseSystem.Web.Mvc.Controller class
Generates the response to the browser request
11
public class HomeController : Controller{
public ActionResult Index(){
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About(){
return View();}
}
ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
12/36
Controller Actions
Public method of the Controller class
Cannot be overloaded
Cannot be a static method
Returns action result
12
public ActionResult About()
{return View();}
ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
13/36
Action Results
Controller action response to a browserrequest
Inherits from the base ActionResult class
Different results types
1ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
14/36
Implement a Controller -
DEMO
14ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
15/36
Action Results Types
ViewResult
EmptyResult
RedirectResult
JsonResult
JavaScriptResult
ContentResult
FileContentResult
FileStreamResult
FilePathResult
1ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
16/36
Controller base class
methods View Redirect
RedirectToAction
RedirectToRoute
Json
JavaScriptResult
Content
File
16ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
17/36
Views
Most of the Controller Actions return views
The path to the view is inferred from the nameof the controller and the name of thecontroller action.
\Views\ControllerName\ControllerAction.aspx
A view is a standard (X)HTML document thatcan contain scripts.
script delimiters in the views
17ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
18/36
Pass Data to a View
With ViewData: ViewData["message"] = "Hello World!";
Strongly typed ViewData:
ViewData.Model = OurModel;
With ViewBag:
ViewBag.Message = "Hello World!";
18ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
19/36
Post data to a controller
Verb Attributes
The action method in the controller accepts thevalues posted from the view.
The view form fields must match the same
names in the controller.
1ventsypopov.com
[HttpPost]public ActionResult Edit(Movie movie){if (ModelState.IsValid){
db.Entry(movie).State = EntityState.Modified;db.SaveChanges();return RedirectToAction("Index");
}return View(movie);}
7/27/2019 ASP.net Mvc Part 1
20/36
Explore a View - DEMO
20ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
21/36
HTML Helpers Methods which typically return string.
Used to generate standard HTML elements textboxes, dropdown lists, links etc.
Example: Html.TextBox() method
Usage is optional You can create your own HTML Helpers
21ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
22/36
Validation
Two types of validation error messages
generated before the HTML form fields arebound to a class
generated after the form fields are bound to the
class
Model State
Validation Helpers
Html.ValidationMessage()
Html.ValidationSummary()
22ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
23/36
Implement validation-
DEMO
2ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
24/36
Routing
The Routing module is responsible formapping incoming browser requests toparticular MVC controller actions.
Two places to setup:
Web.config file
Global.asax file
24ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
25/36
Routing Setup
Web.config file
2
ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
26/36
Routing SetupGlobal.asax file
26
public class MvcApplication : System.Web.HttpApplication{public static void RegisterRoutes(RouteCollection routes)
{routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default","{controller}/{action}/{id}",new { controller = "Home",
action = "Index", id = "" });
}
protected void Application_Start(){
RegisterRoutes(RouteTable.Routes);}
}
ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
27/36
URL Example
http://www.mysite.com/Home/About/6
{controller} = Home
{action} = About{id} = 6
27ventsypopov.comventsypopov.com
7/27/2019 ASP.net Mvc Part 1
28/36
Routing example - DEMO
28ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
29/36
Unit Tests
Used for the business logic (not DAL or Viewlogic).
Test individual unitof code
Make the code safe to modify
Mock Object framework
When you lack real objects
Create mocks for the classes in the application
Test with mock objects
2ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
30/36
Unit Tests - DEMO
0ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
31/36
View Engines
Handles the rendering of the view to UI(html/xml);
Different view engines have different syntax
ASP.NET MVC 3 Pre-included View Engines:
Web Forms
Razor
1ventsypopov.com
7/27/2019 ASP.net Mvc Part 1
32/36
View Engines - DEMO
2ventsypopov.com
hi b
7/27/2019 ASP.net Mvc Part 1
33/36
Things to remember
What MVC stands for
How ASP.NET MVC differs from Web Forms
Where is routing configured
How to validate business logic
How to use helpers
Unit tests basics
Choice between View Engines
ventsypopov.com
f l i
7/27/2019 ASP.net Mvc Part 1
34/36
Useful sites
http://www.asp.net/mvc
http://msdn.microsoft.com/en-us/library/dd394709.aspx
http://stackoverflow.com/
http://jquery.com/
4ventsypopov.com
http://www.asp.net/mvchttp://msdn.microsoft.com/en-us/library/dd394709.aspxhttp://msdn.microsoft.com/en-us/library/dd394709.aspxhttp://stackoverflow.com/http://jquery.com/http://jquery.com/http://jquery.com/http://stackoverflow.com/http://stackoverflow.com/http://msdn.microsoft.com/en-us/library/dd394709.aspxhttp://msdn.microsoft.com/en-us/library/dd394709.aspxhttp://msdn.microsoft.com/en-us/library/dd394709.aspxhttp://msdn.microsoft.com/en-us/library/dd394709.aspxhttp://www.asp.net/mvchttp://www.asp.net/mvc7/27/2019 ASP.net Mvc Part 1
35/36
Questions?
ASP.NET MVC
ventsypopov.com
Email: vepopov [at] microsoft.comTwitter: @v_popov
Ti k )
7/27/2019 ASP.net Mvc Part 1
36/36
Time to wake up :)