aspmvc4 start101
DESCRIPTION
MVC4 IntroTRANSCRIPT
![Page 1: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/1.jpg)
C# ASP.NET-MVC4Frameworks101
Sept 06, 2014By Rich Helton
![Page 2: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/2.jpg)
ASP.NET MVC
Is Microsoft's framework in support of the Model-View-Controller, the most popular design pattern in the world for rapid development.Entity Frameworks are used heavily with ASP.NET MVC, but they are a completely separate framework that are used independently as well.
![Page 3: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/3.jpg)
MVC Components
![Page 4: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/4.jpg)
ASP.NET MVC benefits
Enables Test-Driven Development (TDD).Provides rapid development for developing
ASP.NET in Visual Studio.Supports IIS backend code for enhanced functionality. Provides clean separation of concerns(SoC) between different components.
![Page 5: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/5.jpg)
Enhanced Software Quality
ASP.NET MVC supports the following features for quality:– Security – Has built-in security.– Extensibility – many extensible frameworks.– Testability – supports unit testing
![Page 6: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/6.jpg)
What is TDD?
Test-Driven Development (TDD) is a software development process that relies on the repetition of a very short development cycle by utilizing automated test cases that defines new functionality and programming to the passing these tests.
![Page 7: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/7.jpg)
Installation
We can use the http://www.microsoft.com/web/downloads/platform.aspx
![Page 8: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/8.jpg)
Through the web installer
We can install some pieces to use through WPI:– SQL Express LocalDB Edition 11.0 for data– Visual Studio Express 2012 for coding– ASP.NET MVC 4 the framework– IIS Express for running and deployment
![Page 9: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/9.jpg)
Creating a MVC 4 project
![Page 10: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/10.jpg)
There are several templates
![Page 11: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/11.jpg)
The Empty template
The Empty template does not generate any sample models and controllers:
![Page 12: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/12.jpg)
The Basic template
The Basic template does not generate any sample models and controllers, but some starter pages:
![Page 13: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/13.jpg)
The Internet template
The Internet template now offers basic account views, controllers and models, that can be plugged into a database.
![Page 14: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/14.jpg)
Internet template tests
The Internet template now offers Unit testing into its home controller as well that was generated.
![Page 15: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/15.jpg)
The Intranet template
The Intranet template is similar to the Internet template, except it is gearing its authentication towards Windows authentication. There are no separate account pieces, but a readme.txt describing how to setup authentication in IIS.
![Page 16: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/16.jpg)
Internet basic walkthrough
Sept 06, 2014By Rich Helton
![Page 17: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/17.jpg)
The Internet template
Lets start with an application that we call InternetMVC4App:
![Page 18: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/18.jpg)
Running the App
Running the app from Visual Studio already gives us pieces:
![Page 19: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/19.jpg)
Its all about routing
We will route through the pages, the route begins with the RouteConfig.cs, this defines the starting action to be the Index function in the HomeController.cs:
![Page 20: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/20.jpg)
Controllers are always first
The Controller function is first, in this case, the Index function that returns values, in the form of models, for the pages. A controller can take in a HTTP request or model as needed.
![Page 21: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/21.jpg)
Controllers call Views
The Controller will call views, here we have a layout for all pages that put in the scripts.
![Page 22: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/22.jpg)
Starting a Register
Clicking on the Register link will bring us to the register function
in the AccountController.cs by the link <li><a href="/Account/Register" id="registerLink">Register</a></li>
![Page 23: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/23.jpg)
Register Controller
The link will call the Register function:
Which in turn will call the Register View:
![Page 24: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/24.jpg)
Register View
The Register View will populate the RegisterModel with username and password to pass to a Register function with the model:
![Page 25: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/25.jpg)
Register Model
The associated RegisterModel, notice the data annotations to provide validation on the data:
![Page 26: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/26.jpg)
Register data annotation
The data annotation ensures that the password is at least 6 characters:
![Page 27: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/27.jpg)
Register function note
After a successful register, we will be logged in by way of the Register(RegisterModel model). This is an HttpPost from the page, meaning data is posted.
![Page 28: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/28.jpg)
Just a touch of Controller security
Did you notice the [ValidateAntiForgeryToken], which is available functionality to block cross-site request forgeries and raise an error if the cookie value doesn't match the form value.There is a lot of security and validation functionality that MVC 4 provides.
![Page 29: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/29.jpg)
The membership
Within the App_Data directory of this project is an MDF data file that contains the basic tables for registration. The values will be added as usernames are added:
![Page 30: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/30.jpg)
Defining the default connection
Within the Web.config is where many of the configurations are defined, including the DefaultConnection connection
string:
![Page 31: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/31.jpg)
Setting the Membershipdatabase
Sept 06, 2014By Rich Helton
![Page 32: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/32.jpg)
Run aspnet_regsql.exe
Navigate to the following directory on the server: C:\Windows\Microsoft.NET\Framework\v4.0.30319. Locate "aspnet_regsql.exe", right click and run as
administrator.
![Page 33: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/33.jpg)
Select the SQLExpress DB
We can select the local DB of SQLExpress.
![Page 34: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/34.jpg)
The aspnetdb is created
An aspnetdb database will be created to store users and roles.
![Page 35: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/35.jpg)
Change the connection string
We modify the connection string in web.config to point at SQLEXPRESS. See http://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx
![Page 36: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/36.jpg)
Register the user again
We need to add a user to the new tables through the registration process.
![Page 37: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/37.jpg)
The user is added in SQLEXPRESS
The new user is added to SQLExpress.
![Page 38: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/38.jpg)
Adding Controllers
Sept 06, 2014By Rich Helton
![Page 39: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/39.jpg)
So far...
Just a note, that so far, we have done almost no coding for our solution, but we have functionality for users and registration, as well as some basic pages.
![Page 40: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/40.jpg)
We can add various components
We can add specific components using Visual Studio wizards for Views and Controllers.
![Page 41: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/41.jpg)
Adding a controller
We get several templates to chose from when adding a controller
![Page 42: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/42.jpg)
Controller Templates
Empty MVC controller MVC controller with read/write actions and views, using Entity Framework MVC controller with empty read/write actions Empty API controller API controller with read/write actions and views, using Entity Framework API controller with empty read/write actions
![Page 43: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/43.jpg)
Empty controller
Just provides an Index() action
![Page 44: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/44.jpg)
MVC read/write actions
Provides Index(), Create(), Edit(), and Delete() actions.
![Page 45: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/45.jpg)
MVC with entities
We need to have an EF dbcontext and models define. See
http://www.slideshare.net/rhelton_1/entity-frameworks101
![Page 46: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/46.jpg)
Address Table entity model
We add an Address table
![Page 47: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/47.jpg)
MVC Entity Controller
We can now use the Controller with entities
![Page 48: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/48.jpg)
MVC read/write actions with entities
Provides Index(), Create(), Edit(), and Delete() actions, now with an Address table in these functions.
![Page 49: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/49.jpg)
Showing the access to the Address table
We can see the Address table being accessed in the actions.
![Page 50: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/50.jpg)
Views were added
Views to match these actions to return the entities were added by default
![Page 51: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/51.jpg)
We can add the MVCEntities...
We can add these views that were created to the _Layout.cshtml.
![Page 52: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/52.jpg)
Which will add to the layout
The _Layout.cshtml will show the link.
![Page 53: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/53.jpg)
The Index page
This Index page that was generated is already functional that we see when clicking the link. We didn't code much for this.
![Page 54: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/54.jpg)
Empty WebApi controller
The empty web API controller will create a controller derived from the ApiController, which returns serialized data, such as a string, instead of the Controller interface which returns action results for views.
![Page 55: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/55.jpg)
API controller with read/writes
This will create a sample template for Get(), Put(), Post( ), and Delete() functions using strings.
![Page 56: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/56.jpg)
API controller with entity read/writes
This will create a sample, based on the entity selected, the template for Get(), Put(), Post( ), and Delete() functions using the entity, in this case the Address.
![Page 57: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/57.jpg)
So far....
So far, we have added a lot of controllers, some who connect and pull data from the database.We still haven't coded much.
![Page 58: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/58.jpg)
Adding Views
Sept 06, 2014By Rich Helton
![Page 59: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/59.jpg)
We can add Views
We can create a view to automatically populate with a template of a model for various actions...
![Page 60: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/60.jpg)
Views
When we create the view, it is just the view, and while it may put model information in the view, the controller still has to be created to match the view.
![Page 61: AspMVC4 start101](https://reader035.vdocument.in/reader035/viewer/2022070319/557cb644d8b42ab37c8b4ab0/html5/thumbnails/61.jpg)
Done for now, more to follow
Sept 06, 2014By Rich Helton