Konrad Viltersten Konrad Viltersten - 26 days ago 7
C# Question

Can't find method app.UseStaticFiles()

I'm following this guide and in step 4, I'm asked to add three lines to the project.json file (which I did and then ran dotnet restore getting a lot of updated packages).

When I enter the three lines in the Configure method, I get red lines on all of them. The methods aren't recognized, no intellisense provided etc.

I also noticed that in the example in the guide, the method signature only takes one parameter of IApplicationBuilder, whereas the one I got generated (using the yo aspnet command) looks like this.

Configure(IApplicationBuilder, IHostingEnvironment, ILoggerFactory);


I'm not sure how to resolve it. My guess is that there's a new version of something in the process (Yo, Generators, Core etc.) but I'm not entirely sure.

I've also found this blog where the method signature resembles the one I'm getting. However, the author of it suggest the same syntax that doesn't work for me. I'm guessing it's a matter of referencing the wrong libraries. How do I approach the issue?

Answer

Judging from the screenshots in the linked tutorial, its about ASP.NET Core RC1 (back then called ASP.NET 5 r1-final). You can easily recognize this on the package and namespace names. Microsoft.AspNet.* is used until rc1.

Starting with RC2 the packages were renamed to Microsoft.AspNetCore.* to make it clearer its a new framework and not that much compatible with legacy ASP.NET.

The UseIISPlatformHandler() isn't there anymore, it's now UseIISIntegration() within the Main(...) method:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

And the packages the package is Microsoft.AspNetCore.Server.IISIntegration": "1.0.0" and "Microsoft.AspNetCore.Server.Kestrel": "1.0.1". For static files it's: "Microsoft.AspNetCore.StaticFiles": "1.0.0".

For the Configure overload: Configure(IApplicationBuilder); is default one, but you can add any other type which is registered with the dependency injection system (in ConfigureServices method), as it's a convention system (the startup.cs).