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 pas开发者_JS百科s that over? should i do this on teh client side (jquery) or on the asp.net-mvc server side??
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 }
)
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);
}
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>
精彩评论