C# dotnet core 2 将数据从 middleware/filter 传递到控制器方法
C# dotnet core 2 pass data from middleware/filter to controller method
目前我们正在使用 dotnet core 2 编写 Web 应用程序。
我们实际上创建了某种多主机平台,我们可以在其中根据传递给我们应用程序的 url 注册新客户端。
但是目前我们想创建一个 middleware/filter 来验证我们客户的。
实际上我们想做的是从数据库中拉取一个对象并检查它是否存在,如果存在,我们想调用控制器方法并使该对象可访问,如果不存在,我们实际上想中止并显示错误页面。
我们已经完成的是创建一个 filter/middleware 来实现这一点,但是我们无法找到一种方法来访问我们已经在控制器中 filter/middleware 中拉取的对象方法。
是否真的有相关文档?
我实际上试图从以下方面找出答案:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware?tabs=aspnetcore2x
https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters
但他们并没有描述一种方法来做到这一点,只是为了实际做某事 before/after 动作。
一种方法(不是说它是唯一的或最好的)是让 DI 注入对象的代理,你在中间件中设置对象的真实值,然后你可以从控制器中的代理。
注意:如果您将在方法调用中传递代理对象而不是控制器,请不要忘记使用 [FromServices]
属性对其进行标记。
另一种方法是将对象添加到请求项 属性。但是当你阅读它时,你需要从 object
转换为你实际的 class.
您可以使用 HttpContext.Items
将对象添加到上下文中,文档指出:
The Items collection is a good location to store data that is needed only while processing one particular request. The collection's contents are discarded after each request. The Items collection is best used as a way for components or middleware to communicate when they operate at different points in time during a request and have no direct way to pass parameters.
例如,在你的中间件中:
public class MySuperAmazingMiddleware
{
private readonly RequestDelegate _next;
public MySuperAmazingMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext context)
{
var mySuperAmazingObject = GetSuperAmazingObject();
context.Items.Add("SuperAmazingObject", mySuperAmazingObject );
// Call the next delegate/middleware in the pipeline
return this._next(context);
}
}
然后稍后在您的操作方法中,您可以读取值:
var mySuperAmazingObject = (SuperAmazingObject)HttpContext.Items["mySuperAmazingObject"];
目前我们正在使用 dotnet core 2 编写 Web 应用程序。
我们实际上创建了某种多主机平台,我们可以在其中根据传递给我们应用程序的 url 注册新客户端。
但是目前我们想创建一个 middleware/filter 来验证我们客户的。
实际上我们想做的是从数据库中拉取一个对象并检查它是否存在,如果存在,我们想调用控制器方法并使该对象可访问,如果不存在,我们实际上想中止并显示错误页面。
我们已经完成的是创建一个 filter/middleware 来实现这一点,但是我们无法找到一种方法来访问我们已经在控制器中 filter/middleware 中拉取的对象方法。
是否真的有相关文档?
我实际上试图从以下方面找出答案: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware?tabs=aspnetcore2x https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters
但他们并没有描述一种方法来做到这一点,只是为了实际做某事 before/after 动作。
一种方法(不是说它是唯一的或最好的)是让 DI 注入对象的代理,你在中间件中设置对象的真实值,然后你可以从控制器中的代理。
注意:如果您将在方法调用中传递代理对象而不是控制器,请不要忘记使用 [FromServices]
属性对其进行标记。
另一种方法是将对象添加到请求项 属性。但是当你阅读它时,你需要从 object
转换为你实际的 class.
您可以使用 HttpContext.Items
将对象添加到上下文中,文档指出:
The Items collection is a good location to store data that is needed only while processing one particular request. The collection's contents are discarded after each request. The Items collection is best used as a way for components or middleware to communicate when they operate at different points in time during a request and have no direct way to pass parameters.
例如,在你的中间件中:
public class MySuperAmazingMiddleware
{
private readonly RequestDelegate _next;
public MySuperAmazingMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext context)
{
var mySuperAmazingObject = GetSuperAmazingObject();
context.Items.Add("SuperAmazingObject", mySuperAmazingObject );
// Call the next delegate/middleware in the pipeline
return this._next(context);
}
}
然后稍后在您的操作方法中,您可以读取值:
var mySuperAmazingObject = (SuperAmazingObject)HttpContext.Items["mySuperAmazingObject"];