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

Asked

Viewed 3,066 times

6

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

I thought creating a project in Visual Studio would create a Hello World. but he created an entire application, has several folders, over 200 files, and that’s pretty crazy.

I wanted something very simple I know I can’t do just one Console.Write("Hello World"), but what would be the simplest possible MVC code? I wanted to have very small codes and with few files, I wanted a very small project, almost empty.

All the research I do gives very large codes.

  • You could add in the question what procedure you used in VS to create this project?

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 Controllers, Models and Views folders.

I’m using Visual Studio 2013 Express for Web.

Create a new ASP.Net Web Application project: inserir a descrição da imagem aqui

Add basic MVC references (libraries, Global.asax, Routeconfig, etc.) inserir a descrição da imagem aqui

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 application Asp.NET. See how the structure looks: inserir a descrição da imagem aqui

No views, no controls, no script folder (where we usually have javascript files) and no image folder, ie no files "unnecessary".

To create a Hello World with a View

Now just create a new Controller : inserir a descrição da imagem aqui

I use the MVC 5 Controller - Empty inserir a descrição da imagem aqui

See that a controller was created, which I called Defaultcontroller, very simple: inserir a descrição da imagem aqui

Now we have a Controller with an action called Index, but we still need to create a view for this Action. To do this, just right-click on the Action Index and choose Add View. Fill the screen below: inserir a descrição da imagem aqui As a result you have a cshtml file that is the view of the Controller Defaultcontroller’s Index action. inserir a descrição da imagem aqui

Now just start debug, F5, and you’ll see a beautiful blank page. To include Hello World, simply write in the HTML file Index.cshtml the word Hello World.

If I don’t even want to create a View for my Hello World

If you don’t even want to have a View file, you can choose to do a Hello World as Laerte demonstrated in its reply:

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

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

  • I’ll try that I think Voce understood what I want

  • Now I got what I wanted, didn’t even need the model. just got a doubt have some of those few files that are left that you can erase without fear?

  • Do you really want zero files huh? But you can delete the files .config (Web.config, package.config). You need Global.asax (the main application, where the application starts). Routeconfig.Cs only if you put it on Global.asax.

  • is what I imagined does not need to be zero rsrs but I want to start with the minimum and go putting more as I need and go learning from scratch

8

With Visual Studio open press: Ctrl + Shift + J or can simply create a New Project, select the Web tab and select the ASP MVC 4 Web Application as shown in the following image: inserir a descrição da imagem aqui Then in the next window you choose Internet Application, with the Homecontroller file opened you will add the code below, as shown in the image, then just run the program and access the method by URL.

inserir a descrição da imagem aqui

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

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

So just access the URL from your local server: /Home/Helloworld and you’ll see on the screen.

  • there’s no way to start without those 200 files next door?

  • There, on the second screen instead of choosing the "Internet Application" template, choose: "Empty", from this you can create your own Controllers, Views, etc. Using the "Empty" template (empty) it only creates the essential folders.

  • for example without mvc I can have a program.Cs file and some other project files some 3 or 4 only but the program itself is just a file. Do you understand? I want to know something you can do with a few files, I don’t know, 10, 15, not 200. I could not do anything in this link you passed I think this is paid

  • Yes, now to download the booklets of K19 you have to register, but I believe you are confused how to work with MVC, it is not something like a Console application, each Controller is like a "screen" of the system, and each action is a functionality of the "screen", for you to work with MVC you need to have a Controller that is where you will have the Actions that performed actions, such as saving, deleting, etc.

  • that I understood why I said I do not expect it to be a file with only one class and one method but 200 files is a lot, I wanted something smaller, the smallest possible, I doubt that not to make smaller than this

  • @fabianorastone The smallest template I know is "Empty", the only way to do what you want is for you to create a project and add the references later and your question is how to show a "Hello, world", if you have other questions ask another question.

  • 1

    but that is not a hello world, I did not ask how to write hello world on the screen, I asked how to make a small code that makes only the minimum necessary, the code that ta there in the visual studio has a lot of thing that until I know nothing I know that I do not need

  • 1

    What you have there are Actions, you can remove, or as I mentioned create a project with empty template and create your own controllers.

  • 1

    Here’s the Solution print with the empty template: http://i.stack.Imgur.com/56fgE.png just the essentials, now the Controllers, Views, Models, you create yourself.

  • @Laerte maybe what he wants is not to use MVC, maybe he doesn’t even know what is MVC, in case Fabiano you can create a aspx and work from this point.

  • @Guilhermenascimento It’s really hard to come to terms with what he really wants.

  • I want to use the Asp.net mvc with as little as possible, without these 200 files, I know I will need at least 3 files to do the M, V, and C, I saw how they work, I saw the project example of visual studio, I think I got it. i think only these 3 files do not give need of others and this is what I wanted to know which files are required, which classes are mandatory p/ make only the "hello word" appear anything else but the way Asp.net mvc. I don’t want to authenticate anything I don’t want it to be cute I don’t want you to do anything else

  • The @Laerte already showed how to do what you want, create a project with the Empty/Empty template, see the image that he posted some comments above: http://i.stack.Imgur.com/56fgE.png

  • this Empty template will create only one class without walk or will make a minimal mvc Asp.net project?

  • It will create a standard structure for you to be able to use MVC normally, read: the basics to work.

  • 1

    my mistake is that I was creating the wrong tempolate Guilherme solved

Show 11 more comments

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 requirement, it is only a standard project software. To get a sense of this you see that there are other types of "patterns":

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

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

Therefore the minimum possible yet yes will still generate many files (emphasizing: you are not obliged to understand the meaning of each of them).

I’ll be honest I even understand you, I’m very similar, I like to be in "control", avoid letting Ides do my job (I think that’s how you think), but unfortunately there doesn’t seem to be enough clear material on how to create your own MVC (that works with "routes" of course), at first edit the file HomeController.cs would be the right thing to do, but if you really need alternatives, I believe this can help you:

Note: Unfortunately in Portuguese I was unable to find anything.

If you want to try MVVM:

  • I’ll see your links, I understand the mvc design pattern I understand that can not do with 1 file but the other guilerme showed that can do much smaller than what u were doing

  • I didn’t ask for 3 files, I know I need more but 200 was a lot of things but taking the files from the project is now with 4 files is better than I expected and I think we can do with 3 if not do the view as Guilherme said

  • The 3 files I read in some comment, the moment you stop doing the view, you will have more difficulty working with the answers of the "routes"

1

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

Take a look at this tutorial from Microsoft Hello World Tutorial.

And if you want to get off to a really good start, study the new ASP.NET vNext, you will need to download the Visual Studio 2015 Preview or the KVM. He (Visual Studio) also has a Class Library project and will help you work with new ways to recover dependencies. (Bower and Grunt as JS Task Runner)

  • I’m trying to use vnext but gave an error already asked about it but nobody answered. I wanted to start off the way you said and go putting extra things to the few like putting a very simple model after

  • 1

    Are you using Visual Studio? If so, which version?

  • 2015 check out http://answall.com/questions/45248/aspnetmvc6-n%C3%A3o-can-update#45248

  • The answer you received there is valid. To migrate a project from ASP.Net 5 to 6 is not very simple. Even more than now VS has a new Pilot. Go to File > New Project > Visual C# > Web > ASP.NET 5 Class Library.

  • I’ll try that

Browser other questions tagged

You are not signed in. Login or sign up in order to post.