How to make a Hello World in ASP.NET MVC?

6

I already program but I'm starting with C # and ASP.NET MVC. I wanted to make a Hello World program and add things gradually as I always did with any language I've learned.

I thought creating a project in Visual Studio would create a Hello World . but it has created an entire application, it has several folders, more than 200 files, and this is very crazy.

I wanted something very simple I know that you can not only do a Console.Write("Hello World") , but what would be the simplest MVC code possible? I wanted to have very small code with few files, I wanted a very small, almost empty project.

Every search I make gives very large codes.

    
asked by anonymous 29.12.2014 / 17:44

4 answers

9

To create a simple structure for an ASP.NET MVC site

The simplest for a Hello World.

Some folders are mandatory, such as the Controllers, Models, and Views folders.

I'm using Visual Studio 2013 Express for Web.

Create a new ASP.Net Web Application project:

AddthebasicMVCreferences(Libraries,Global.asax,RouteConfigandetc.)

As a result, you will have a very basic solution. One that if you run F5, will not display any page because there are no pages, but there is the Asp.NET application. Here's how it looks:

Noviews,nocontrols,noscriptfolder(whereweusuallyhavejavascriptfiles)andnoimagefolder,ieno"unnecessary" files.

To create a Hello World with a View

Now you just need to create a new Controller:

AndIusetheMVC5Controller-Empty

See that a controller was created, which I called the DefaultController, very simple:

NowwehaveaControllerwithanactioncalledIndex,butwestillneedtocreateaviewforthisAction.Todothis,justrightclickontheActionIndexandchooseAddView.Fillinthescreenbelow: As a result you have a cshtml file which is the view of the Index action of the Controller DefaultController.

Nowjuststartthedebug,F5,andyou'llseeabeautifulblankpage.ToincludeHelloWorld,simplywritetheHelloWorldwordintheHTMLoftheIndex.cshtmlfile.

IfIdonotevenwanttocreateaViewformyHelloWorld

IfyoudonotevenwanttohaveaViewfile,youcanchoosetodoaHelloWorldasLaertedemonstratedinyouranswer:

publicstringHelloWorld(){return"Hello World";
}

So, just start the application and navigate to http://localhost:<suaPorta>/Default/HelloWorld

    
29.12.2014 / 19:25
8

With Visual Studio open, press: Ctrl + Shift + J or simply create a New Project select the Web tab and select the ASP MVC 4 Web Application, as shown in the following image: TheninthenextwindowyouchooseInternetApplication,withtheHomeControllerfileopenyouwilladdthecodebelow,asshownintheimage,thenjustruntheprogramandaccessthemethodbytheURL.

By default, when you create a ASP MVC Web Application , it also creates a standard Controller ("HomeController") , you can create an Action which is a method within Controller :

public string HelloWorld()
{
    return "Hello World";
}

So you just have to access the URL by your local server: / Home / HelloWorld and you will see the screen.

    
29.12.2014 / 17:51
2

MVC is not something specific to ASP.NET, so much so that it is totally possible to work without MVC because it is not a must, it is just a standard of project software. To get a sense of this see that there are other types of "patterns":

In languages like PHP, those who do not use framework will rarely use MVC, but this is not the language issue.

It's almost impossible to create an MVC (in your case it looks like it's an MVC and Routes) with 3 files in asp.net mvc as you requested, as several settings are needed, as an example file Web.config and all these files to which you refer are parts of the settings necessary for everything to work and you are not obliged to understand one by one.

So the minimum possible will still still generate many files (pointing out: you are not required to understand the meaning of each one of them).

I'll be honest, I even understand you, I'm very similar, I like to be in control, to avoid letting IDEs do my work (I think that's how you think), but unfortunately there does not seem to be material that is clear enough how to create your own MVC (working with "routes" of course), at first editing the HomeController.cs file would be the most correct, but if you really need alternatives, I believe this can help you:

  

Note: Unfortunately in Portuguese I was not able to find anything.

If you want to try MVVM:

29.12.2014 / 19:49
1

You have created an MVC Web project. Start with a simple Class Library .

Take a look at this Microsoft tutorial Hello World Tutorial .

And if you want to get started right now, study the new ASP.NET vNext , you will need to download Visual Studio 2015 Preview or KVM . It (Visual Studio) also has a Class Library type project and will already help you work with new ways to recover dependencies. (Bower and grunt as JS Task Runner)

    
29.12.2014 / 18:41