leora
leora

Reputation: 196679

How can I track which page I am on in asp.net-mvc

I have a link in a master page that says "Send feedback" which brings up a new web page.

When I go to that new page, I have a form and I want to populate a textbox with the URL that the person was on when they clicked the "Send Feedback" button.

How can I grab the current URL to pass that over? Should I do this on the client side (jQuery) or on the asp.net-mvc server side?

Upvotes: 1

Views: 173

Answers (3)

Mikael Östberg
Mikael Östberg

Reputation: 17166

There is no need to pass along the URL as you can check the UrlReferrer property on the Request object in the "Send feedback" action.

Use this as your controller action:

public ActionResult SendFeedback(string message) {
   var referrer = Request.UrlReferrer;
   feedbackService.Send(message, referrer);
}

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

You could pass Request.Url.RawUrl as query string parameter to the Send feedback action which could itself store it as a hidden field in the form which will be reposted back and allowing to redirect back to the original page once the feedback is submitted. Example:

@Html.ActionLink(
    "Send feedback", 
    "Index", 
    "Feedback", 
    new { returnUrl = Request.Url.RawUrl }
)

Upvotes: 7

Chandu
Chandu

Reputation: 82933

You can do it Server Side(preferred) using

Request.UrlReferrer

For implementing it client side, use a hidden variable and then use a form post.

something like:

<input type="hidden" id="referredPage" name="referredPage"/>

<script type="text/javascript">
    $(function(){
        $("form").submit(function(){
            $("#referredPage").val(window.location.href);
        });
    })
<script>

Upvotes: 1

Related Questions