Begin with this answer . I'll start from her to continue.
The basic structure of an ASP.MET MVC Project
I created my project, I can connect it to a SQL Server Express database, and I have something very similar to this:
- App_Data:Directorythatstoreslocaldatabases,suchasLocalDb;
- App_Start:Classesinvokedtostarttheapplication;
- Content:Filesthatarenormallypartofthepresentation,suchasimagesandstylesheets(CSS);
- Controllers:Thecontrollingentitiesoftheapplication.Theyorganizetherequestsmadetothesystemandharmonizethedataentities(Models),
- fonts:Fontsdirectory;
- Migrations:Directorythatstoresincrementaldatabasemigrations.UsuallyusedwiththeEntityFramework,whichisinstalledbydefaultintheASP.NETMVCproject;
- Models:Thedescriptionofthedataentitiesandtherelationshipsbetweenthem.WithinaDDDconcept,itwouldbethedomainoftheapplication;
- Scripts:Directoryforpresentationprogramming.UsuallysavesJavaScriptfiles;
- Views:Directoriesandfilesofthepresentationlayer.ItisnotnecessarilyHTML,butbydefaultitis.ViewsarewrittenusingtheRazornotation.
Hello,World(orequivalent)inMVC5
AsIchosetheASP.NETIdentityoption,3Controllersarecreated:
AccountController.cs
;HomeController.cs
;ManageController.cs
;
WhatisalwayscreatedisHomeController.cs
.Itusuallylookslikethis:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;namespaceTeste.Controllers{publicclassHomeController:Controller{publicActionResultIndex(){returnView();}publicActionResultAbout(){ViewBag.Message="Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
The Index
method would be the closest to a "Hello, World" that we can have. When we start the site, it is the first request the system receives. Try running and putting a breakpoint on return View()
to confirm this.
Basic Cycle
In the case of Index
, which has a statement only:
public ActionResult Index()
{
return View();
}
We ask you to return a View without giving anything to it. Since we do not enter its name, MVC will look at Views/Home
for a file named Index.cshtml
. If it does not find it, it will look for other extensions whose name starts with Index
and then searches for Index
within Views/Shared
.
In fact, it's the% system's main layout , ready-to-login screens, and a default error screen.
Notice that the method returns a Views/Shared
. ActionResult
can be many things:
- A View ;
- A redirect to another Action ;
- One file;
- A response to a raw request;
- A JSON;
- An XML;
- etc, etc., etc.
I'm not going to go into details about this so I do not overstretch the question.
ViewBag
Notice that in other methods we have the use of ActionResult
:
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
ViewBag
is an auxiliary dynamic object to populate View with some useful information. Should not be used for persistent information . It's great for messaging, handy handles, and small controls. Never should be used for complex business rules.
And now?
We have several questions that can serve as next steps in your learning. We have the ASP.NET MVC website with your tutorials . The tag asp.net-mvc has an internal Wiki with more information and material for study. Need more, you can open a question or chat and take your question with us .