app.UseMiddleware() causing 500

2019-09-12 07:21发布

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

3条回答
The star\"
2楼-- · 2019-09-12 08:03

You can also just call:

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

instead of the UseMiddleware methods.

查看更多
该账号已被封号
3楼-- · 2019-09-12 08:16

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.

查看更多
聊天终结者
4楼-- · 2019-09-12 08:16

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();
查看更多
登录 后发表回答