Get URL of referring page and pass it to a hidden form field - url

I need to know how to get the url of the referring page of my website. When a user clicks a link to a contact form on my website (from another page on my website) how can I get the url of that page? I have tried document.referrer but it only shows the domain name of my website, I need to see what page a visitor was viewing when they clicked on the link to the contact form. My site is static html, css and javascript.
Also tried setting a cookie, but it only shows the page where the contact form is actually located, for example: www.mysite.com/contact-us.html. What I need to know is what page the visitor was viewing when they clicked on the link that brought them to the form on the contact page.
My objective is to pass the url into a hidden form field, so when submitted, I can tell which page of my website the user was viewing when they clicked on the link to the contact form. Then, if they ask a question about a specific product, I will know which page they were on when they viewed the product. Note: I have 100+ product pages that link to the contact form.

If you have a master page used by all the product pages, you can save the current url as session in master page when the product page was loaded . Then you can retrieve the session url when contact page was loaded. This should able do the work.

You can use the HttpRequest.UrlReferrer property.
Gets information about the URL of the client's previous request that linked to the current URL.
public partial class Contact : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Uri urlReferrer = Request.UrlReferrer;
if (urlReferrer != null)
{
hiddenFieldUrlReferrer.Value = urlReferrer.ToString();
}
}
}

Related

Hiding URL Parameters from Controller to View: MVC 5

Say I have a website with a wide variety of different pages, each tailored for an individual user and accessible only by that user. When a user accesses a page, the URL for that page will be UserStuff/Pages/11, where 11 is the page's ID. Right now, because users can have multiple pages, I am using an ActionLink element to direct the user to their selected page, like so:
// In Index.cshtml
// ...
#Html.ActionLink("View Page", "Pages", new { id = page.ID }, new { #class=...})
The controller intercepts the given id:
public ActionResult Pages(int id)
{
// ...
Page page = db.Pages.Find(id);
return View(page);
}
This works well and delivers exactly how I want the application to behave. However, I wish for the ID to be hidden in the URL. How do I pass information from the View to the controller via a parameter without having it exposed in the URL? Say that I don't want the Page's ID visible in the URL, how can I link to a different Action that takes the given parameter?
I believe that keeping the request a GET request is important due to other actions redirecting to the resulting page. Wrapping it in a form/POST request would make said actions unable to access the information without having to go through the same process as the original user, which is not something that I want in this implementation.

MVC return view and navigate to page within that view

Using C# MVC. I need to return the view if the user enters invalid information into the form.
Code is:
if (!ModelState.IsValid) {
return View();
}
But the Home view contains several pages within it that can be navigated to (#about, #services, #gallery, #contact). So once the page reloads it goes directly to the Home page (when I click Contact it navigates there and I can see the error messages) but I need it to navigate to the Contact page automatically so the user can see their error message and how to fix it.
I have tried
string url = Request.UrlReferrer.AbsolutePath+ "#contact";
return redirect(url);
This redirects the page to #contact BUT does not show the error message.
Any other suggestions, kindly assist

Passing and Storing objects between Pages

I'd like some advice on this issue. Basically, I have 2 pages (for simplicity sake), and the second page is completely dependent on the first page.
So lets say the main page is a search page, and the second page is a view page.
The site works off XML requests, and the ultimate aim is to keep the XML requests to a minimum as each request is a little slow. I considered using Sessions, but I've had some problems in the past with sessions being mixed between users randomly which i only manage to curb by changing the recycle process in IIS to a very small time frame, so I'm wondering if there is any other way. I've tried TempData, but that expires after one request, so a page refresh on the view page doesn't seem possible. (or is it?)
Anyways, we have the Search page that has, say, 5 attributes that are required by the View page, but only 2 are needed to make the XML request on the view page.
e.g.
Search Page Contains:
ID,
Name,
City,
Email,
Height
View Page needs the following from the Search page to complete the xml request:
ID,
Name,
Email
View Page displays all the information from the search page, plus everything in the XML response.
The link i have in the search page only has the ID in the url, so name and email are required for the XML request on the second page some how. Not sure if it's possible without sessions?
What i've tried is:
Store the search results in TempData. That way, when someone clicks the 'View' link (View), the View page loads the search results like so:
var viewPage = SearchResults.Where(w => w.ID == id).FirstOrDefault();
The ViewModel then renders the page by grabbing the Name and Email from viewPage, making an XML request, and displaying the response, along with other required details from viewPage.
It works as expected with tempdata. Data only persists on the first request, dies on a page refresh. Sessions is the alternative, but is there any other?
(sorry for the wall of text :)
Why not using more standard techniques like a <form> tag in the search page pointing to the controller action that will perform the search:
#using (Html.BeginForm("search", "somecontroller", FormMethod.Get))
{
... some input fields for your search criteria
<button type="submit">Search</button>
}
and then you will have the Search controller action:
public ActionResult Search(SearchModel model)
{
var results = ....
return View(results);
}
I've used GET method on the form which allows the user to bookmark the results page and come back to it later.

How to get last page visited in a controller

is there a way to get or store the last page visited? Example if I'm on a List Page with a New link that loads a page with form. If the user cancels, you go back to the previous page (List Page).
Thanks
Well, in web forms, you would use: Request.UrlReferrer
http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx
So I assume you can also use that in MVC, accessible through the HttpContext property of the RequestContext acessible by teh controller.

How to create a view that is not accessible directly but can only be redirected to?

I'm currently working on the user registration in my project. After the registration is done I wish to show some confirmation to the user. I decided to create another view. That's fine.
Now, if after the registration I just return the view like:
public class MyController : Controller
{
[AcceptVerbs (HttpVerbs.Post), ValidateAntiForgeryToken]
public ActionResult Registration (FormCollection form)
{
/* Some logic goes here */
return View ("ConfirmationView");
}
}
Everything is working as desired. No changed url in the title bar. But... If I click the refresh button, the browser will submit the data from the form again which I do not want.
Then I decided to create a separate action, but that means it will produce a new url in the address bar. I do not want the user to click refresh now because this view will not be able to sensibly display the confirmation information again. Is there any way to make an action not accessible directly? Or at least any way to determine whether it was called directly or by redirection? In the latter case I would just take the user away from that page to maybe the home page.
Any way to accomplish this?
So I found the solution myself.
One can use TempData to detect the repeated or external action calls.
public class MyController : Controller
{
[AcceptVerbs (HttpVerbs.Post), ValidateAntiForgeryToken]
public ActionResult Registration (FormCollection form)
{
/* Some logic goes here */
TempData["RedirectCall"] = true;
return RedirectToAction ("Confirmation");
}
[AcceptVerbs (HttpVerbs.Get)]
public ActionResult Confirmation ()
{
if (TempData["RedirectCall"] == null)
return RedirectToAction ("StartPage", "Home");
return View ();
}
}
Nice and simple. :)
One way to solve your problem is to attach a guid or similar type of "random" data to a user session, and check for a valid session when the page is requested. If there is none, you redirect to a page saying that this url is not available at the moment, and that the user will soon be redirected (and then redirect to home after 5 seconds or so using js).
Roughly it would work like this:
When the user is registered, a session cookie is created with for example a GUID. The GUID is also stored in a database table, in which you have one column for the UserID primary key and one for the GUID. You also create an authentication cookie, thus logging the user on to your site.
When all datacalls etc are done, the user has been successfully registered and so on, you redirect to the confirmation page.
When the confirmation page is loaded, the user is automatically logged on (because you created the authentication cookie in step 1). You can then check for a row in the UserID-GUID table corresponding to the logged on user.
a) If there is such a row, you delete the row, and display the confirmation page with all the information.
b) If there is no such row, you display the error message and redirect. As you deleted the row when you showed the message the first time, the user will not be able to access the confirmation page again.
Note: If you use this approach (or some other that makes the confirmation page available only once) you should make sure that it is clearly stated on the confirmation page that the user won't be able to access that page again.
if(Request.UrlReferrer == null)
{
return RedirectToAction("Index");
}

Resources