0

I am trying to get the url of my current webpage.

I am using asp.net with MVC.

When I debug the code,see below, currentUri value is https://localhost:44377/. Should really be https://www.google.ca/. Please help.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        Response.Redirect("https://www.google.ca/", true);
        string currentUri = HttpContext.Request.Url.AbsoluteUri;

        return View();
    }
 }
rafCalg
  • 13
  • 5
  • 1
    It shouldn.t be `https://www.google.ca/`, because the request actually still the one on `localhost` until your browser redirect to google. And once it redirected, you still cannot get the request `AbsoluteUri` because it is out of your control then. So you cannot get the `AbsoluteUri` of the external URL you will redirect to. You should find another way to do that. – Shawn Xiao May 21 '20 at 00:43

2 Answers2

0

Request.Url is url of the current request as server itself sees it. It is not necessary url that user used in they browser to look at the page (most sites have one or more "proxy" layers between server and user) and it definitely not a url of that will be set on response to be rendered as destination of 30x redirect.

If you are debugging locally the Request.Url is pretty much guaranteed to be localhost. Using IIS + hosts file you can assign local DNS name, but that still will be url of request and it can't be changed by code of the server (only configuration how server is actually receiving urls).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

The url returned belongs to the location from which the function HttpContext.Request.Url.AbsoluteUri is being called. In this case is from the Index() located at your HomeController which is running in your local machine https://localhost:44377/. The website https://www.google.ca/ is another website which your code doesn't have access to.