app.UseMiddleware() causing 500

2019-09-12 07:46发布

问题:

If I call the middleware directly in my startup.cs, e.g.:

public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware<StaticFileMiddleware>();
    app.UseMiddleware<DefaultFilesMiddleware>();
}

Looking at the source on GitHub, this is exactly the code that the extension methods are calling. However, I'm getting a 500 error and a stack trace:

 :(
Oops.
500 Internal Server Error
System.InvalidOperationException
The 'Invoke' method's first argument must be of type 'HttpContext'.
at Microsoft.AspNetCore.Builder.<>c__DisplayClass3_0.<UseMiddleware>b__0(RequestDelegate next)
at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Show raw exception details

System.InvalidOperationException: The 'Invoke' method's first argument must be of type 'HttpContext'.
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass3_0.b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

This might be related to my No Static Files Extensions are referable question.

Either way, I currently can't get a simple index.html page to load successfully.

Any help?

Thanks

回答1:

You're probably using the RC1 version of the StaticFiles library inside of your project, instead of RC2.

In project.json, change this:

"dependencies": {
   "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final"
}

To this:

"dependencies": {
   "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final"
}

The namespace also changed, but once that's corrected you should be good to go.



回答2:

Since now the asp.net core 1.0 has been released. The correct NuGet package is: (notice: .AspNetCore.)

"Microsoft.AspNetCore.StaticFiles": "1.0.0"

and you can use (again)

app.UseStaticFiles();


回答3:

You can also just call:

app.UseStaticFiles();
app.UseDefaultFiles();

instead of the UseMiddleware methods.