app.UseMiddleware<T>() 导致 500

app.UseMiddleware<T>() causing 500

如果我直接在我的startup.cs中调用中间件,例如:

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

查看 source on GitHub,这正是扩展方法调用的代码。但是,我收到 500 错误和堆栈跟踪:

 :(
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()

这个可能与我的No Static Files Extensions are referable问题有关。

无论哪种方式,我目前都无法成功加载简单的 index.html 页面。

有什么帮助吗?

谢谢

您可能在项目中使用的是 RC1 版本的 StaticFiles 库,而不是 RC2。

project.json 中,更改为:

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

为此:

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

命名空间也发生了变化,但一旦更正,您就可以开始了。

您也可以直接拨打:

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

而不是 UseMiddleware 方法。

自此 asp.net 核心 1.0 已经发布。 正确的 NuGet 包是:(注意:.AspNetCore。)

"Microsoft.AspNetCore.StaticFiles": "1.0.0"

你可以(再次)使用

app.UseStaticFiles();