asp.net cookie .
aspx default.aspx , .
script default.aspx:
<script src="https://code.jquery.com/jquery-2.2.0.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$("a").click(function (e) {
e.preventDefault();
var attrHref = $(this).attr("href");
$.getJSON("/service.asmx/SetRouteCookie", { href: attrHref }, function (e) {
window.location.reload();
});
});
});
</script>
script , click .
click - ajax. cookie :
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false, UseHttpGet = true)]
public void SetRouteCookie()
{
if (HttpContext.Current.Request.QueryString["href"] != null)
{
string href = HttpContext.Current.Request.QueryString["href"];
HttpCookie c = new HttpCookie("CurrentRoute", href);
c.Expires = DateTime.Now.AddHours(1);
HttpContext.Current.Response.Cookies.Add(c);
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write("{\"status\":\"ok\"}");
}
}
cookie javascript.
Page_Load :
protected void Page_Load(object sender, EventArgs e)
{
#region process route
if (HttpContext.Current.Request.Cookies["CurrentRoute"] != null)
{
var route = HttpContext.Current.Request.Cookies["CurrentRoute"].Value.ToString();
string pageName = GetPageName(route);
Placeholder1.Controls.Add(LoadControl("/ctrls/" + pageName + ".ascx"));
}
else
{
Placeholder1.Controls.Add(LoadControl("/ctrls/default.ascx"));
}
#endregion
}
public string GetPageName(string href)
{
int index = href.IndexOf("&");
if (index == -1)
return href.Trim('/');
else
{
return href.Substring(0, index).Trim('/');
}
}
git:
HideRoute