This post is an addition to the 5 part tutorial series on getting started with ASP.NET MVC. This isn't really another part, but a prequel describing setting up a basic ASP.NET MVC project. This will be helpful if you're using an alternative to Visual Studio, such as VS Code (which is available for Linux, Mac, or Windows), or if you just want to know more about how to do things without the use of templates in Visual Studio.
The Files
The first file we'll look at is the Program.cs file, which contains the start up code for our web application:
Program.cs
csharp
using System.IO;
using Microsoft.AspNetCore.Hosting;namespace MeowWorld
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
This will build a new WebHost object, then run it.
We want it to use Kestrel and Internet Information Services (IIS). IIS is Microsoft's main server software and most likely what you're using to run this application in a Windows environment. Kestrel is a lightweight server that can run on a variety of environments. Kestrel lacks many of the features of IIS and Microsoft suggests running both IIS and Kestrel for this reason. Depending on your situation, you may want one or the other, or both. I recommend both unless you have a lot of experience with this already.
By default, we set the root directory to the current directory, but we can change that to another path if we want to store our web content elsewhere.
Finally we set a startup object that contains methods to customize the WebHost object more. These methods can be moved to the Program.cs file and that can be used as the StartUp class, but Visual Studio puts them in their own file for organization purposes, so that's what I'll be doing too.
For changing the port setting on this file to work in OpenShift, see Part 3.
Startup.cs
csharp
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;namespace MeowWorld
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
}
Here we have two methods for setting up the WebHost. We use the ConfigureServices method for adding services to be used and the Configure method to configure them and other settings.
For example, if you look at the if check in the Configure method, you'll see how it checked if this is a development build and if so, shows the error 500 page that contains info like a stack trace.
Now here's where things can get a little overwhelming.
project.json
json
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.Mvc": "1.0.1",
},"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
I'm not going to break down everything in here, but I will explain the important parts. Also, if you're using NuGet in Visual Studios, a lot of this should be self-managing.
dependenciesis where you put the libraries you'll be using. As you can see, there are only four to get started with MVC and what each one is for should be self-explanatory.-
toolsis for Visual Studio add-ons to help with development. If you're not using Visual Studio, you don't have to worry about this. Even if you are, you may not want to bother adding something here depending on how useful you find it. -
frameworksis simply what .NET library you're working with. This will probably only be changed when you want to upgrade a project's .NET version. -
Next is
buildOptions,runtimeOptions, andpublishOptions. This is where configuration options related to build and deployment go. -
Then we have
scripts. This is for commands you'd like to run at various steps of building and deploying. You can see the current value will deploy the compiled code to the IIS server. -
And finally we have the web.config file:
web.config
xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>
Before project.json everything was stored in an XML config file like this one. Unfortunately, some things are still in here for legacy's sake (such as project settings for IIS), but fortunately these are settings you'll probably never have to change. So I don't overwhelm you with information you probably won't need, I won't spend time on this file other than saying you'll likely need to include it as-is.
Conclusion
I hope that this is helpful for getting started with ASP.NET for those of you using Visual Studio Code over the traditional VS. Visual Studio for Mac was recently announced, so who knows if Linux may be next?
Let me know in the comments if you found this tutorial for VS Code helpful, and what you'd like to see more of regarding ASP.NET and OpenShift!
Check out the previous posts in this series:
저자 소개
유사한 검색 결과
Key considerations for 2026 planning: Insights from IDC
Red Hat and Sylva unify the future for telco cloud
Edge computing covered and diced | Technically Speaking
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래