Skip to content

githubsman/MvcMovie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ASP.NET Tutorial from Microsoft

Use MVC 5 and .NET 8 to put together a Model-View-Controller framework in C#.

Part 1: First app

  1. Prereqs
    • Localhost (IIS)
    • Visual Studio 2022
    • NET 8.0
  2. Create a working basic app
  3. Enjoy an out-of-box experience

Part 2: Controllers

  • HTTP GET
  • Routing pattern /[Controller]/[ActionName]
  • Model binding and MapControllerRoute
  • Input sanitization using HtmlEncoder.Default.Encode

Part 3: Views

  1. Add a view (Razor)
    • HTTP POST
    • Action methods
    • IActionResult Interface
    • return View();
  2. Change views and layout pages
    • Views/Shared/_Layout.cshtml
    • @RenderBody()
    • Layout = ...
  3. Change the title, footer, and menu link in the layout file
  4. Passing Data from the Controller to the View

Part 4: Models

The second half of this page does a good job tying together pattern concepts.

  1. Add a data model class
    • Entity-Framework Core
    • Data structure via POCO (Plain Old CLR Objects) classes
  2. Scaffold movie pages
    • Scaffolding an EF to enable CRUD
    • The data context
    • Supporting pages with action methods
    • Database connectivity
  3. Initial migration
    • NuGet Package Manager Console
    • Add-Migration InitialCreate
    • Update-Database
  4. Test the app
    • Routing pattern /[Controller]/[ActionName]/[Parameters]
  5. Dependency injection in the controller
    • "Kids have it so easy these days."
    • #MARK Dependency injection ... is used in this constructor
    • Management of connection string
  6. Strongly typed models and the @model directive
    • #MARK MVC 5 feature: ViewData dictionary is a dynamic object...

Part 5: SQL

  1. SQL Server Express LocalDB
    • Using the SQL Server Obj eXplorer pane
    • View/Edit tables using View Designer from SSOX
    • View/Edit Data using View Data from SSOX
  2. Seeding the database

This page pulls together many pattern concepts.

  • GET and POST methods compared
  • Generated HTML
  • Tag helpers (meaning, HTML tag)
  • [Bind] ... effective against over-posting
  • Preventing request forgeries: ValidateAntiForgeryToken
  • Scaffolding and views
  1. Processing the POST Request
  • Client-side data validation
  1. Add Search by genre
  • LINQ query
  • .Contains(searchString)
  • Minimal user input form (GET)
  1. Add search by genre to the Index view
  • Evaluation VS Inspection

Part 8: Add a field

  1. Add a Rating Property to the Movie Model
  • Three approaches to data schema changes:
    • Drop the database and recreate it with seed data
    • Explicitly modify the schema
      • manually (so they call it)
      • using a database change script
    • Use Code First Migrations

Part 9: Validation

  • Keeping things DRY
    • rules established at the model layer
    • data-entry validation provided for both record creation and editing
  • Delete the previously edited data
    • the app pulls from the seed data on next launch
  1. Add validation rules to the movie model
  • using DataAnnotations namespace
    • DataType
    • DisplayFormat
    • Required
  1. How validation works (jQuery)
  • ModelState.IsValid
  • Server- and client-side (redundant) operations
    • testing on server-side only
  1. More about DataType Attributes
  • Range
  • Regular expressions for validation
  • FirstOrDefaultAsync
  • For staged usage of a URL (two methods with same name and signature), how to satisfy CLR requirements:
    1. add differing ActionName parameters, or...
    2. add a contrived notUsed parameter to one.

--

Directory structure

Folder PATH listing
Volume serial number is AA35-206F
C:.
|   .gitattributes
|   .gitignore
|   MvcMovie.sln
|   MvcMovies_tree_build.txt
|   README.md
|   
+---.github
|   \---workflows
\---MvcMovie
    |   appsettings.Development.json
    |   appsettings.json
    |   MvcMovie.csproj
    |   MvcMovie.csproj.user
    |   Program.cs
    |   
    +---bin
    |   \---Debug
    |       \---net8.0
    |           |   appsettings.Development.json
    |           |   appsettings.json
    |           |   
    |           |   drivers ... 220+ .dll files
    |                   
    +---Controllers
    |       HelloWorldController.cs
    |       HomeController.cs
    |       MoviesController.cs
    |       
    +---Data
    |       MvcMovieContext.cs
    |       
    +---Migrations
    |       20240701174435_InitialCreate.cs
    |       20240701174435_InitialCreate.Designer.cs
    |       MvcMovieContextModelSnapshot.cs
    |       
    +---Models
    |       Class.cs
    |       ErrorViewModel.cs
    |       Movie.cs
    |       
    +---obj
    |   |   MvcMovie.csproj.nuget.dgspec.json
    |   |   MvcMovie.csproj.nuget.g.props
    |   |   MvcMovie.csproj.nuget.g.targets
    |   |   project.assets.json
    |   |   project.nuget.cache
    |   |   
    |   \---Debug
    |       \---net8.0
    |           |   .NETCoreApp,Version=v8.0.AssemblyAttributes.cs
    |           |   apphost.exe
    |           |   MvcMovie.AssemblyInfo.cs
    |           |   MvcMovie.AssemblyInfoInputs.cache
    |           |   MvcMovie.assets.cache
    |           |   MvcMovie.csproj.AssemblyReference.cache
    |           |   MvcMovie.csproj.BuildWithSkipAnalyzers
    |           |   MvcMovie.csproj.CoreCompileInputs.cache
    |           |   MvcMovie.csproj.FileListAbsolute.txt
    |           |   MvcMovie.csproj.Up2Date
    |           |   MvcMovie.dll
    |           |   MvcMovie.GeneratedMSBuildEditorConfig.editorconfig
    |           |   MvcMovie.genruntimeconfig.cache
    |           |   MvcMovie.GlobalUsings.g.cs
    |           |   MvcMovie.MvcApplicationPartsAssemblyInfo.cache
    |           |   MvcMovie.pdb
    |           |   MvcMovie.RazorAssemblyInfo.cache
    |           |   MvcMovie.RazorAssemblyInfo.cs
    |           |   MvcMovie.sourcelink.json
    |           |   project.razor.vs.bin
    |           |   staticwebassets.build.json
    |           |   staticwebassets.development.json
    |           |   staticwebassets.pack.json
    |           |   
    |           +---ref
    |           |       MvcMovie.dll
    |           |       
    |           +---refint
    |           |       MvcMovie.dll
    |           |       
    |           +---scopedcss
    |           |   +---bundle
    |           |   |       MvcMovie.styles.css
    |           |   |       
    |           |   +---projectbundle
    |           |   |       MvcMovie.bundle.scp.css
    |           |   |       
    |           |   \---Views
    |           |       \---Shared
    |           |               _Layout.cshtml.rz.scp.css
    |           |               
    |           \---staticwebassets
    |                   msbuild.build.MvcMovie.props
    |                   msbuild.buildMultiTargeting.MvcMovie.props
    |                   msbuild.buildTransitive.MvcMovie.props
    |                   msbuild.MvcMovie.Microsoft.AspNetCore.StaticWebAssets.props
    |                   
    +---Properties
    |       launchSettings.json
    |       serviceDependencies.json
    |       serviceDependencies.local.json
    |       serviceDependencies.local.json.user
    |       
    +---Views
    |   |   _ViewImports.cshtml
    |   |   _ViewStart.cshtml
    |   |   
    |   +---HelloWorld
    |   |       Index.cshtml
    |   |       Welcome.cshtml
    |   |       
    |   +---Home
    |   |       Index.cshtml
    |   |       Privacy.cshtml
    |   |       
    |   +---Movies
    |   |       Create.cshtml
    |   |       Delete.cshtml
    |   |       Details.cshtml
    |   |       Edit.cshtml
    |   |       Index.cshtml
    |   |       
    |   \---Shared
    |           Error.cshtml
    |           _Layout.cshtml
    |           _Layout.cshtml.css
    |           _ValidationScriptsPartial.cshtml
    |           
    \---wwwroot
        |   favicon.ico
        |   
        +---css
        |       site.css
        |       
        +---js
        |       site.js
        |       
        \---lib
            +---bootstrap
            |   |   LICENSE
            |   |   
            |   \---dist
            |       +---css
            |       |
            |       |                            | x | .rtl.min.css
            |       |       bootstrap            | x | .rtl.css.map
            |       |       bootstrap-grid       | x | .rtl.css
            |       |       bootstrap-reboot     | x | .min.css.map
            |       |       bootstrap-utilities  | x | .min.css
            |       |                            | x | .css.map
            |       |                            | x | .css   
            |       |       
            |       |       
            |       \---js
            |               bootstrap            | x | 
            |               bootstrap.min        | x | 
            |               bootstrap.esm        | x | 
            |               bootstrap.esm.min    | x | 
            |               bootstrap.bundle     | x | 
            |               bootstrap.bundle.min | x | .js
            |               bootstrap            | x | .js.map
            |               bootstrap.min        | x | 
            |               bootstrap.esm        | x | 
            |               bootstrap.esm.min    | x | 
            |               bootstrap.bundle     | x | 
            |               bootstrap.bundle.min | x | 
            |               
            +---jquery
            |   |   LICENSE.txt
            |   |   
            |   \---dist
            |           jquery.js
            |           jquery.min.js
            |           jquery.min.map
            |           
            +---jquery-validation
            |   |   LICENSE.md
            |   |   
            |   \---dist
            |           additional-methods.js
            |           additional-methods.min.js
            |           jquery.validate.js
            |           jquery.validate.min.js
            |           
            \---jquery-validation-unobtrusive
                    jquery.validate.unobtrusive.js
                    jquery.validate.unobtrusive.min.js
                    LICENSE.txt                    

About

Tutorial MVC 5 using .NET 8

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published