3

I'm trying to refresh a page when clicking tag href.

But I could not get the current page URI in my MVC Razor view and How I get the current page URI and how I refresh the same page?

e.g.: www.local.url/articles

I need: local.url/articles and local.url/articles?querystring

2 Answers 2

2

If you only want to refresh the querystring and nothing else, you don't have to write the url for the current page, you can just write the querystring part:

click <a href="?test">here</a> to set the querystring to "?test" in your current url

If for some reason you still want to add the current url to the link though, you can do it by either finding it from the current request url or from the httpcontext

@{ 
  var option1 = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item);
  var option2 = System.Web.HttpContext.Current.Request.Url;
}
1
  • 1
    Link Manager is working but it takes base url from site file. not locally running site. i need locally running site
    – Rajasekar
    Commented Apr 7, 2019 at 13:35
-1

You can redirect user to same page by following the steps:

  1. Add the javascript function:
function RefreshPage(queryString) {
    var newUrl = window.location.origin + window.location.pathname + "?key=" + queryString;
    window.location.href = newUrl;
    return false;
}
  1. Use the JavaScript function in html href tag in view: For example i added in aboutus controller method http://localhost:49422/Home/About
<a href="javascript:void(0)" onclick="RefreshPage('@ViewBag.Key')"  class="btn btn-primary btn-lg">Redirect To Current Page &raquo;</a>

3.Generate the Random string and pass to ViewBag. This is just for the example purpose , you can pass value from Model also: Reference https://stackoverflow.com/questions/1344221/how-can-i-generate-random-alphanumeric-strings

public string GetUniqueKey(int size)
{
    char[] chars =
                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
    byte[] data = new byte[size];
    using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
    {
        crypto.GetBytes(data);
    }
    StringBuilder result = new StringBuilder(size);
    foreach (byte b in data)
    {
        result.Append(chars[b % (chars.Length)]);
    }
    return result.ToString();
}
  1. After following above steps, when user will click on the Href tag then in the url you will see:
http://localhost:49422/Home/About?key=<dynamic string>
3
  • @Dan Sinclair: Thanks for the content formatting. Can you please guide me,how to add code block, i already checked the posts but not able to make it?
    – Amit Kumar
    Commented Apr 8, 2019 at 17:13
  • Check out the editing help section of the site: sitecore.stackexchange.com/editing-help#code Commented Apr 8, 2019 at 18:12
  • I will check the details.
    – Amit Kumar
    Commented Apr 8, 2019 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.