You can redirect user to same page by following the steps:
- Add the javascript function:
function RefreshPage(queryString) {
var newUrl = window.location.origin + window.location.pathname + "?key=" + queryString;
window.location.href = newUrl;
return false;
}
- 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 »</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();
}
- 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>