How to Get Url Referrer in ASP.NET Core MVC

How can I get Url Referrer in ASP.NET Core MVC?

You're almost there. The StringValues class is just a type ASP.NET uses to efficiently represent strings in the framework. Especially in the HttpContext object. You can just call ToString() on it to convert it to a string:

string referer = Request.Headers["Referer"].ToString();

Url Referrer in ASP.NET Core MVC not working

Dashboard.cshtml

<div class="text-center">
<h1 class="display-4">Dashboard</h1>
@{
var parms = new Dictionary<string, string> { { "from", Context.Request.Path } };
}
<a asp-controller="Home" asp-action="DetailsPage" asp-all-route-data="parms">DetailsPage</a>
</div>

HomeController.cs

public IActionResult DetailsPage(string from)
{
return View();
}

DetailsPage.cshtml

<div class="text-center">
<h1 class="display-4">DetailsPage</h1>
<button onclick="window.history.back();">Go Back</button>
</div>

Repo

The original url(Dashboard/Home/Teacher), could be passed as parameter to controller. It could be further passed to the view.

Getting referrer in asp net mvc

Usually, Referrer URLs are passed between two unrelated sites (from one site to another) when navigation occurs by clicking a link or JavaScript-based navigation. Referrer URLs are not sent if the user uses the browsers address bar, back/forward buttons/ etc.. to navigate.

There are several reasons why the Referrer URL is empty in a request.

  • For some (security/privacy) reasons, the Referrer URL is stripped out
    when navigating from a HTTPS site to a HTTP site (e.g. from
    https://google.com to http://example.com).

  • It can also be stripped out using some other JavaScript
    and HTML tricks.

once Referrer URL has been stripped out, There is no way to disable this behavior to get it back.

Redirect to previous page URL in asp.net core using MVC - c#

You should pass your current location when you are navigating to next location, something like returlurl in login. There is no any built-in feature for this purpose.

<a href="/nexlocation?returnurl=currenturl">my link</a>

Asp.Net Core url referer is empty

You are almost there. Try this:

string referer = Request.Headers["Referer"].ToString();

Getting the HTTP Referrer in ASP.NET

You could use the UrlReferrer property of the current request:

Request.UrlReferrer

This will read the Referer HTTP header from the request which may or may not be supplied by the client (user agent).

get referrer from a specific page in same domain C#

I was able to figure it out. Took some parts of Albert's code and made some changes to mine.

      Uri referrer = HttpContext.Current.Request.UrlReferrer;

string urlName = Request.UrlReferrer.ToString(); // grabbing referring page address

if (referrer == null && urlName != "default.aspx")
{
Response.Redirect(url: "default.aspx", endResponse: false);
return;
}

if (!IsPostBack)
{
if(Session["customerID"] == null && urlName != "default.aspx") //If both are false they go to homepage
{
Response.Redirect(url: "default.aspx", endResponse: false);
}
else
{
customerInfo(); //or else they get the customer info on the customer page
}
}

How do I get the referrer URL in an ASP.NET MVC action?

You can use Request.UrlReferrer to get the referring URL as well if you don't like accessing the Request.ServerVariables dictionary directly.

MVC 5: Obtaining the Refering URL

Request.UrlReferrer will give you a Uri object when you are visiting the current page from a link in another web page. If you are directly accessing the url ( as you do when you hit F5 button in Visual studio), you will get a null value as the return value of Request.UrlReferrer call as there we are directly going to this page.

To verify this you can do this.

Have 2 action method

public ActionResult Index()
{
var r = Request.UrlReferrer;
return View();
}
public ActionResult About()
{
return View();
}

Now in the about view(~/Views/Home/About.cshtml), add this makrup to generate a link to your index action.

@Html.ActionLink("Index","Index","Home")

Put a breakpoint in the Index action so you can inspect the r varibale value.
Run your app. Go to About page, Click on your Index link and see what value you get in the r variable when the breakpoint hits the Index action.



Related Topics



Leave a reply



Submit