How to start in MVC5 ASP.NET?

2

Hello! About a month or so I started to venture into the C # world and then started studying the .NET Framework. Soon after, I also studied ASP.NET because of the need in my company. Today I use Visual Studio 2013 to create Web Apps with the MVC standard, of which I have worked for years in PHP.

Visual Studio creates all directories, URL paths, among other configuration files, that's fine. But I'd like to actually build once at least one .NET application on the MVC 5 standard once, to really know how the Framework works. Would anyone have any book, website tip, functions from which I would have to start studying?

For example, in C code and in other languages the application starts with void main . In ASP.NET how does the configuration to initialize the application work. And how do I make the Routes file, anyway. Basically how I make the application work.

I know that in a production environment I would not create in the hand, but as a programmer I think it's important to know how things work in the back-end, it's not magic!

Thank you in advance.

    
asked by anonymous 22.05.2015 / 01:06

1 answer

3

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 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 .

    
16.07.2015 / 21:21