Asp.Net Core中的Request.Url.PathAndQuery

时间:2019-05-18 12:19:47

标签: asp.net-core

我从Asp.Net Framework转到了Asp.Net Core。在Asp.Net Core中Request.Url.PathAndQuery的替代属性或等效属性是什么?

3 个答案:

答案 0 :(得分:1)

从 ASP.NET Core 2.0 开始,有一个扩展方法:

public static string GetEncodedPathAndQuery (this Microsoft.AspNetCore.Http.HttpRequest request);

您需要包含此命名空间:

using Microsoft.AspNetCore.Http.Extensions;
...
var pathAndQuery = context.Request.GetEncodedPathAndQuery();

答案 1 :(得分:0)

您需要使用HttpContext分别访问URL路径和查询字符串。

在控制器中:

var path = HttpContext.Request.Path;
var query = HttpContext.Request.QueryString;
var pathAndQuery = path + query;

要获取HttpContext,请参阅How to Access HttpContext in Asp.Net Core

答案 2 :(得分:0)

尝试这个也是我的工作。

using Microsoft.AspNetCore.Http;    
using Microsoft.AspNetCore.Http.Features;   
/// <summary>
/// Gets the raw target of an HTTP request.
/// </summary>
/// <returns>Raw target of an HTTP request</returns>    
/// <remarks>   
/// ASP.NET Core manipulates the HTTP request parameters exposed to pipeline    
/// components via the HttpRequest class. This extension method delivers an untainted   
/// request target. https://tools.ietf.org/html/rfc7230#section-5.3 
/// </remarks>  
public static string GetRawTarget(this HttpRequest request) 
{       
    var httpRequestFeature = request.HttpContext.Features.Get<IHttpRequestFeature>();       
    return httpRequestFeature.RawTarget;    
}
我正在创建扩展方法或Request对象。