ASPCore中间件中的当前URL? [英] Current URL in ASPCore Middleware?

查看:72
本文介绍了ASPCore中间件中的当前URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以访问ASPCore 2.0中间件中的当前请求的URL?

Is there a way I can access the current Requested URL in ASPCore 2.0 Middleware?

有什么可以注射的吗?

推荐答案

HttpContext对象将传递到中间件的Invoke方法.您可以在该属性上访问Request属性.

HttpContext object will be passed to the Invoke method of your middleware. You can access the Request property on that.

您可以使用GetDisplayUrl扩展方法或GetEncodedUrl扩展方法.

You can use the GetDisplayUrl extension method or GetEncodedUrl extension method.

public Task Invoke(HttpContext context)
{
    var url1 =context.Request.GetDisplayUrl();
    var url2  = context.Request.GetEncodedUrl();       


    // Call the next delegate/middleware in the pipeline
    return this._next(context);
}

这两个扩展方法在Microsoft.AspNetCore.Http.Extensions命名空间中定义.因此,请确保您有一个using语句来包含名称空间

These 2 extension methods are defined in Microsoft.AspNetCore.Http.Extensions namespace. So make sure you have a using statement to include the namespace

using Microsoft.AspNetCore.Http.Extensions;

这篇关于ASPCore中间件中的当前URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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