app.UseMiddleware<T>() causing 500

馋奶兔 提交于 2020-01-06 16:16:28

问题


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.



来源:https://stackoverflow.com/questions/37331641/app-usemiddlewaret-causing-500

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!