Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.Url.AbsoluteUri not working in asp.net core 2.0 (Using System.Web)

Please I am trying to get my domain url using the Request.Url.AbsoluteUri in asp.net core 2.0 but "Request" is not in the System.Web namespace. and this is the popular way of achieving this. Please is it different in ASP.NET core 2.0. Please help. Below is the line of code

var verifyUrl = "/user/VerifyAccount/" + activationCode;
var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, verifyUrl);

Normally "link" should give me "http://myDomainName/user/VerifyAccount/276ye6tfhsgt63t6e" but the System.Web.Request is not working

like image 953
James D Avatar asked Dec 06 '25 18:12

James D


1 Answers

The HttpRequest object is different in ASP.NET Core. It no longer contains Url property. You'll need to build your URI manually:

var uriBuilder = new UriBuilder
{
    Scheme = Request.Scheme,
    Host = Request.Host.ToString(),
    Path = $"/user/VerifyAccount/{activationCode}"
};

var link = uriBuilder.Uri.AbsoluteUri;

The Request variable will be available by default in your controller and such. If you find that there is no Request variable, you can try HttpContext.Request. If you're doing this inside a view, you'll need ViewContext.HttpContext.Request.

like image 108
Chris Pratt Avatar answered Dec 09 '25 15:12

Chris Pratt