Home > OS >  How to create Startup class in ASP.NET Core 6 MVC
How to create Startup class in ASP.NET Core 6 MVC

Time:09-27

I am new and am studying ASP.NET Core 6 MVC. I am stuck in this error when building a Startup class like in my tutorial.

Error unable to resolve service for type 'Microsoft.Extensions.FileProviders.IFileProvider'

This is my Program.cs class:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Startup.cs class:

using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Generic;

namespace HCMUE_ASPCore_Lab02
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IFileProvider>
            (
                new PhysicalFileProvider
                (
                    Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")
                )
            );
            services.AddMvc();
        }
    }
}

After reading many posts, I know that ASP.NET Core 6's Program and Startup have changed and my tutorial is old now. But I don't know how to update my Program and Startup to fit with ASP.NET Core 6.

Please help me. Any help would be appreciated.

Thank you for reading.

CodePudding user response:

The error message is saying that your application is trying to create an instance of FileUploadController but it doesn't know how to create an instance of IFileProvider to pass into the constructor.

This is a typical dependency injection error, You can register it in this code:

builder.Services.AddSingleton<IFileProvider>(new PhysicalFileProvider
                (
                    Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")
                ));

You can follow this Docs to learn about Dependency injection and other fundamentals in .Net 6.

If you are not comfortable with the new changes in .Net6, you can add the Startup class according to this article.

CodePudding user response:

You want to ensure that both the static file middleware, and any services that inject your file provider, end up using the same instance.

var provider = new PhysicalFileProvider(
  Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")
);
builder.Services.AddSingleton<IFileProvider>(provider);

...

app.UseStaticFiles(
  new StaticFileOptions{ 
    FileProvider = provider
  });

But if you don't really need a different file provider, you can instead inject IWebHostEnvironment into your controller and reuse the default .WebRootFileProvider.

  • Related