温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET Core的Middleware怎么使用

发布时间:2021-12-06 14:28:55 来源:亿速云 阅读:125 作者:iii 栏目:大数据

本篇内容主要讲解“ASP.NET Core的Middleware怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“ASP.NET Core的Middleware怎么使用”吧!

结构

在ASP.NET Core里,每个从「浏览器传入」的HTTP Request封包,会被系统封装为「HttpRequest对象」,并且配置默认的HttpResponse对象、Session对象、ClaimsPrincipal对象...等等物件。接着将这些对象,封装成为一个「HttpContext对象」,用来提供ASP.NET Core后续使用。

ASP.NET Core在收到HttpContext之后,会把它交给一个「Pipeline」去处理。这个Pipeline里面配置很多「Middleware」。系统会将HttpContext,依序传递给Pipeline里的Middleware去处理。每个Middleware会依照自己内部的程序逻辑,来运算处理HttpContext,并且变更HttpContext所封装的对象内容。

ASP.NET Core在收到经由Middleware处理完毕的HttpContext之后,就会取出其中所封装的HttpResponse对象。然后依照这个HttpResponse对象,来建立从「服务器回传」的HTTP Response封包内容。

ASP.NET Core经由上述的系统结构,完成HTTP Request封包输入、HTTP Response封包输出的工作流程。

开发

在这个范例里,Middleware透过实做Invoke方法,来提供自己所封装的程序逻辑。

public class HelloWorldMiddleware
{
    // Fields
    private readonly RequestDelegate _next;

    // Constructors
    public HelloWorldMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    // Methods
    public Task Invoke(HttpContext context)
    {
        // Response
        context.Response.WriteAsync("Hello World!");

        // Return
        return Task.CompletedTask;
    }
}

在实做Middleware.Invoke方法的时候,开发人员可以透过HttpContext.Request,来取得从「浏览器传入」的HTTP Request封包内容。在下列的范例程序代码里,就是透过HttpContext.Request的Path、QueryString两个属性,来分别取得HTTP Request封包的URL Path与QueryString。

public class HelloWorldMiddleware
{
    // Fields
    private readonly RequestDelegate _next;

    // Constructors
    public HelloWorldMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    // Methods
    public Task Invoke(HttpContext context)
    {
        // Request
        string path = context.Request.Path.ToString();
        string queryString = context.Request.QueryString.ToString();
        string message = string.Format("path={0}, queryString={1}", path, queryString);

        // Response
        context.Response.WriteAsync(message);

        // Return
        return Task.CompletedTask;
    }
}

同样在实做Middleware.Invoke方法的时候,开发人员可以透过HttpContext.Response,来设定从「服务器回传」的HTTP Response封包内容。在下列的范例程序代码里,就是透过HttpContext.Response的WriteAsync方法、StatusCode属性,来分别设定HTTP Response封包的Content与StatusCode。

public class HelloWorldMiddleware
{
    // Fields
    private readonly RequestDelegate _next;

    // Constructors
    public HelloWorldMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    // Methods
    public Task Invoke(HttpContext context)
    {
        // Response
        context.Response.StatusCode = 404;
        context.Response.WriteAsync("Not Found");

        // Return
        return Task.CompletedTask;
    }
}

而在实做Middleware.Invoke方法的时候,如果程序代码里发生了预期之外的Exception。ASP.NET Core预设会使用「500 Internal Server Error」,这个StatusCode来通报系统内部发生异常。 在下列的范例程序代码里,就是直接抛出一个例外错误,交由ASP.NET Core的错误处理机制去处理。

public class HelloWorldMiddleware
{
    // Fields
    private readonly RequestDelegate _next;

    // Constructors
    public HelloWorldMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    // Methods
    public Task Invoke(HttpContext context)
    {
        // Exception
        throw new Exception();

        // Return
        return Task.CompletedTask;
    }
}

建立Middleware的时候,开发人员可以透过建构子所传入的RequestDelegate,来参考到Pipeline里的下一个Middleware。透过调用RequestDelegate,就可以调用Pipeline里的下一个Middleware的Invoke方法。在下列的范例程序代码里,就是透过调用RequestDelegate,来调用Pipeline里的下一个Middleware的Invoke方法,藉此串接其他Middleware的程序逻辑。

public class HelloWorldMiddleware
{
    // Fields
    private readonly RequestDelegate _next;

    // Constructors
    public HelloWorldMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    // Methods
    public async Task Invoke(HttpContext context)
    {
        // Do Something 01
        //....

        // Next
        await _next.Invoke(context);

        // Do Something 02
        // ...
    }
}

到此,相信大家对“ASP.NET Core的Middleware怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI