Asp.net core 2.0 MVC from scratch (empty web project) in VS Code
Visual studio code and asp.net core MVC
Today, I am going to talk about implementing asp.net core in visual studio code.
VS code is a lightweight tool compared to visual studio. It runs fast, is easy to use and with the extensions you can improve your development setup.
Setup
What do we need?
- **vs code (https://code.visualstudio.com/) **
- **.net core 2.0 sdk **
Install the vs code on your computer and follow the .net core setup instructions.
Once dotnet core is installed open command prompt and type :
dotnet -h
you should get an output of dotnet.
Next open visual studio and go to extensions and search for "**the C# extension" **and download it.
If you prefer you can install the visual studio keymap, if you like the shortcuts of visual studio.
create a new project folder for example C:\myApp
**
**
Create an asp.net core mvc app
- Open in visual code your project folder (myApp).
- Go to view "integrated terminal" and type dotnet new web.
- dotnet will install an empty web project the folders will be displayed in vs code.
Next, add two folders Controllers and **Views. **
In the "Views"folder create a sub-folder called Home.
And an Index.cshtml with the text hello world in Views/Home.
**
**
<h1>hello world</h1>
In the "Controllers" folder add a homecontroller.cs
**
**
using Microsoft.AspNetCore.Mvc;
namespace myapp.Controllers{
public class HomeController:Controller{
public IActionResult Index(){
return View();
}
}
}
Next, we have to configure the project so it can use mvc.
Open the **startup.cs file **and add the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace myapp
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
We configured the mvc routing so that it will look for the HomeController with the action method Index when the app runs.
The index method corresponds with the Views/Home/Index.cshtml view in
We're ready, hit** **the **start debugging button **in the "debugging tab" or type
dotnet run
the app will run and you will see the "hello world" text.
References
The whole article is on my blog with more details