Monday, July 18, 2016
In the previous post we have enabled MVC on our application. Now we want to add our first MVC controller and view to test out verify that MVC is working. We also have to tell ASP.NET Core what pattern to look for when looking for our controllers and views.
Step-By-Step Instructions:
1. Add a folder call "Controllers" in the root of your application
2. Right click on the "Controllers" folder and select "Add" → "New Item" → .NET Core → MVC Contoller Class, then click "Add"
3. You see the "HomeController.cs" class being added to the "Controllers" folder
5. Create a folder in the root of the application call "Views" then inside the "Views" folder create another folder call "Home"
6. Right click on the "Home" folder and select Add → New Item → .NET Core → MVC View Page, keep the name of the view as Index.cshtml then click "Add"
7 . Double click on the view "Index.cshtml" and type "Hello World" between the <div> tag like the markup below
8. Press Ctrl+F5 to run the application, you will see the following.
The code above configures the default route for our application with name of the route assigned to "Default", the "template:" field specifies the pattern that MVC should look for when locating our routes. The "defaults:" field uses an anonymous object to define a default route in case nothing is found or nothing is typed into the URL.
11. Now when you press Ctrl+5 you will see our "Hello World"
If you encounter the following error:
Add the line services.AddMvc(); to the ConfigureServices method like so:
Step-By-Step Instructions:
1. Add a folder call "Controllers" in the root of your application
2. Right click on the "Controllers" folder and select "Add" → "New Item" → .NET Core → MVC Contoller Class, then click "Add"
3. You see the "HomeController.cs" class being added to the "Controllers" folder
4. Double click on the "HomeController.cs" file and you will see the following code
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace NorthwindCafe.Web.Controllers { public class HomeController : Controller { // GET: // public IActionResult Index() { return View(); } } }
5. Create a folder in the root of the application call "Views" then inside the "Views" folder create another folder call "Home"
6. Right click on the "Home" folder and select Add → New Item → .NET Core → MVC View Page, keep the name of the view as Index.cshtml then click "Add"
7 . Double click on the view "Index.cshtml" and type "Hello World" between the <div> tag like the markup below
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> Hello World! </div> </body> </html>
8. Press Ctrl+F5 to run the application, you will see the following.
9. All you will see is an empty page, where is our "Hello World"? The reason we don't see our view is because we haven't define any routes for our application yet.
10. To enable routing for our application open up the Startup.cs file and modify the app.UseMvc() method's config instance to configure the routes for our application
public void Configure(IApplicationBuilder app) { app.UseStaticFiles(); app.UseMvc(config => { config.MapRoute( name: "Default", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action = "Index" } ); }); }
The code above configures the default route for our application with name of the route assigned to "Default", the "template:" field specifies the pattern that MVC should look for when locating our routes. The "defaults:" field uses an anonymous object to define a default route in case nothing is found or nothing is typed into the URL.
11. Now when you press Ctrl+5 you will see our "Hello World"
If you encounter the following error:
:(
Oops.
500 Internal Server Error
500 Internal Server Error
System.InvalidOperationException
Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddMvc' inside the call to 'ConfigureServices(...)' in the application startup code.
Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddMvc' inside the call to 'ConfigureServices(...)' in the application startup code.
C:\NorthwindCafe\NorthwindCafe.Web\Startup.cs
Line 17: {
Line 18: app.UseStaticFiles();
Line 19: app.UseMvc(config => {
Line 20: config.MapRoute(
Line 21: name: "Default",
at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder app, Action<IRouteBuilder> configureRoutes)
at NorthwindCafe.Web.Startup.Configure(IApplicationBuilder app) in C:\Users\Jason\Source\Repos\Grades\NorthwindCafe\NorthwindCafe.Web\Startup.cs:line 19
System.Reflection.TargetInvocationException
Exception has been thrown by the target of an invocation.
Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle.InvokeMethod(object target, Object[] arguments, Signature sig, bool constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke(object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
Add the line services.AddMvc(); to the ConfigureServices method like so:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); }
ASP.NET Core Posts:
- How To Create An ASP.NET Core Application From Scratch
- ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
- Enable MVC On ASP.NET Core Application
- ASP.NET Core : Create Our First Controller and View
- ASP.NET Core : Adding The Default View With _ViewStart.cshtml
- ASP.NET Core : Create A Responsive Layout With Bootstrap
- ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons
- ASP.NET Core : Add .json Configuration Files With Microsoft.Extensions.Configuration Library
- ASP.NET Core : Entity Framework Core Models For Northwind Cafe
- ASP.NET Core : Create The NothwindContext ( EntityFrameworkCore )
- ASP.NET Core : Configure project.json File To Support Entity Framework Core
- ASP.NET Core : Add NorthwindContext To Startup Class And Create Database
- ASP.NET Core: Seeding The NorwindCafe Database
- ASP.NET Core: Add Logging To The NorthwindCafe Application
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Aivivu chuyên vé máy bay, tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu
vé từ mỹ về việt nam
có vé máy bay từ nhật về việt nam không
các chuyến bay từ đức về việt nam hôm nay
vé máy bay từ canada về việt nam bao nhiêu tiền
Các chuyến bay từ Incheon về Hà Nội hôm nay
vé máy bay chuyên gia nước ngoài sang Việt Nam