C#dotnet core 2将数据从中间件/过滤器传递到控制器方法 [英] C# dotnet core 2 pass data from middleware/filter to controller method

查看:959
本文介绍了C#dotnet core 2将数据从中间件/过滤器传递到控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们正在编写一个使用dotnet core 2的Web应用程序。

currently we are writing a web application with dotnet core 2.

我们实际上创建了一种多主机平台,可以在其中基于URL注册新客户端。

We actually create some kind of multi-hosting platform where we can register new clients based on the url passed to our application.

不过,目前我们想创建一个中间件/过滤器来验证客户端的内容。

However currently we wanted to create a middleware/filter to validate our client's.

实际上,我们想要做的就是从数据库中拉出一个对象并检查它是否存在,如果是,我们想调用控制器方法并使该对象可访问,如果它不存在,我们实际上想中止并显示错误

Actually what we wanted to do is pull an object from the database and check if it exists, if yes, we want to call the controller method and make the object accessible, if it does not exist, we actually want to abort and show an error page.

我们已经完成的工作是创建一个过滤器/中间件,该过滤器/中间件可以做到这一点,但是我们无法找到一种方法来访问我们已经拉出的对象在控制器方法内部的过滤器/中间件中。

What we already have done is created a filter/middleware that does exactly that, however we couldn't figure out a way to access the object that we already pulled in our filter/middleware inside the controller method.

实际上是否有任何相关文档?

is there actually any documentation for doing that?

I实际上试图从以下位置找出原因:
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware?tabs=aspnetcore2x
https://docs.microsoft.com/zh-CN/aspnet/core/ mvc / controllers / filters

I actually tried to figure it out from: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware?tabs=aspnetcore2x https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters

,但是他们没有描述执行此操作的方法,只是在执行操作之前/之后实际执行操作。

but they don't describe a way to do it, only to actually do something before/after the action.

推荐答案

您可以使用 HttpContext.Items ,其文档状态为:

You could add the object to the context using HttpContext.Items which the docs state:


Items集合是存储只有在处理一个特定请求时才需要的数据的好位置。每次请求后,馆藏的内容都会被丢弃。 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.

例如,在您的中间件中:

For example, in your middleware:

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);
    }
}

然后稍后在操作方法中,您可以阅读值:

Then later on in your action method, you can read the value:

var mySuperAmazingObject = (SuperAmazingObject)HttpContext.Items["mySuperAmazingObject"];

这篇关于C#dotnet core 2将数据从中间件/过滤器传递到控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆