Use MVC 5 and .NET 8 to put together a Model-View-Controller framework in C#.
Part 1: First app
- Prereqs
- Localhost (IIS)
- Visual Studio 2022
- NET 8.0
- Create a working basic app
- 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
- Add a view (Razor)
- HTTP
POST
- Action methods
IActionResult
Interfacereturn View();
- HTTP
- Change views and layout pages
Views/Shared/_Layout.cshtml
@RenderBody()
Layout = ...
- Change the title, footer, and menu link in the layout file
- 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.
- Add a data model class
- Entity-Framework Core
- Data structure via POCO (Plain Old CLR Objects) classes
- Scaffold movie pages
- Scaffolding an EF to enable CRUD
- The data context
- Supporting pages with action methods
- Database connectivity
- Initial migration
- NuGet Package Manager Console
Add-Migration InitialCreate
Update-Database
- Test the app
- Routing pattern
/[Controller]/[ActionName]/[Parameters]
- Routing pattern
- Dependency injection in the controller
- "Kids have it so easy these days."
#MARK
Dependency injection ... is used in this constructor- Management of connection string
#MARK
See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
- Strongly typed models and the
@model
directive#MARK
MVC 5 feature:ViewData
dictionary is a dynamic object...
Part 5: SQL
- 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
- Seeding the database
Part 6: Controllers, cont'd
This page pulls together many pattern concepts.
GET
andPOST
methods compared- Generated HTML
- Tag helpers (meaning, HTML tag)
- [Bind] ... effective against over-posting
- Preventing request forgeries:
ValidateAntiForgeryToken
- Scaffolding and views
- Processing the
POST
Request
- Client-side data validation
Part 7: Search functionality
- Add Search by genre
LINQ
query.Contains(searchString)
- Minimal user input form (
GET
)
- Add search by genre to the Index view
- Evaluation VS Inspection
Part 8: Add a field
- 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
- Add validation rules to the movie model
- using
DataAnnotations
namespaceDataType
DisplayFormat
Required
- How validation works (jQuery)
ModelState.IsValid
- Server- and client-side (redundant) operations
- testing on server-side only
- More about DataType Attributes
Range
- Regular expressions for validation
Part 10: Examine Details
and Delete
methods
FirstOrDefaultAsync
- For staged usage of a URL (two methods with same name and signature), how to satisfy CLR requirements:
- add differing
ActionName
parameters, or... - add a contrived
notUsed
parameter to one.
- add differing
--
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