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?
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();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With