In the Program.cs
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
The UseContentRoot
method in the code above sets the default location to the current directory of your application.
To enable the static file middleware, you can call app.UseStaticFiles();
within the Startup class:
public class Startup
{
...
// This method is used to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
app.UseStaticFiles();
By enabling this middleware, all files in the web root folder (<content root>/wwwroot
) will be served by default.
If you explore the Angular2 template on GitHub, you will notice that the output path specified in webpack.config.js is:
output: {
path: path.join(__dirname, 'wwwroot', 'dist'),
filename: '[name].js',
publicPath: '/dist/'
},
This means the content will be stored in
<content root>/wwwroot/dist
and the URL for these files will look like:
http://<host and port>/dist/<my file>
It's important to note that the URL does not need to include ClientApp
(which is at the content root level and not accessible via static file middleware), but rather just dist
(at the wwwroot level).
To link to images in your application, place them in the ...\wwwroot\images
folder so that the image URLs will follow this format:
http://<host and port>/images/<my image file>
.