Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.Url.PathAndQuery in Asp.Net Core

Tags:

asp.net-core

I moved from Asp.Net Framework to Asp.Net Core. What would be the replacement or equivalent property for Request.Url.PathAndQuery in Asp.Net Core?

like image 664
Ivan Avatar asked Dec 02 '22 10:12

Ivan


2 Answers

From ASP.NET Core 2.0 onwards use the UriHelper.GetEncodedPathAndQuery(HttpRequest) extension method.

It's in the Microsoft.AspNetCore.Http.Extensions namespace.

For example, inside a Controller action method:

using Microsoft.AspNetCore.Http.Extensions;

[...]

[Route("/foobar")]
public IActionResult MyControllerAction()
{
    string pathAndQuery = this.Request.GetEncodedPathAndQuery();
}
like image 93
Ade Stringer Avatar answered Dec 09 '22 13:12

Ade Stringer


You need to access url path and query string separately using HttpContext.

In controller:

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

To get HttpContext, refer to How to Access HttpContext in Asp.Net Core

like image 33
Ryan Avatar answered Dec 09 '22 15:12

Ryan