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

查看:25
本文介绍了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?

我实际上试图通过以下方式弄清楚:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware?tabs=aspnetcore2xhttps://docs.microsoft.com/en-us/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天全站免登陆