2016-05-04 47 views
1

我有一個MVC網站,我正在使用Cookie來存儲網站主題。我有以下功能來設置Cookie ...這是駐留在_Layout.vbhtml這是我的所有網站的「母版頁」。餅乾不是全網

function setCookie(cname, cvalue, exdays) { 
     var d = new Date(); 
     d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); 
     var expires = "expires=" + d.toUTCString(); 
     document.cookie = cname + "=" + cvalue + "; " + expires; + "path=/"; 
    } 

即使我指定了根路徑,我的Cookie並不總是在站點範圍內工作。

的餅乾在兩個地方......在_Layout.vbhtml我也有一個設置...

$(document).ready(function() { 
     //Set User Theme Preferences 
     //First check theme cookie and if null Or empty set to default values 
     var $theme = getCookie('myTheme'); 

     if ($theme == "") { 
      //theme cookie does Not exists Or has expired 
      setCookie('myTheme', 'bootstrap', 90); 
      $theme = "bootstrap"; 
     } 

     if ($theme != "bootstrap") { 
      //theme is not bootstrap so change css link 
      $("#cssTheme").attr("href", ('@Url.Content("~/Content/bootstrap.min.css")').replace("bootstrap", $theme)); 
     } 
    }) 

而且我也有家庭/ Index.vbhtml此點擊功能 - 唯一的地方,用戶可以更改主題。

$("button[data-theme]").click(function() { 
      $theme = $(this).data("theme") 
      setCookie('myTheme', $theme, 90); 
      $("head link#cssTheme").attr("href", ('@Url.Content("~/Content/bootstrap.min.css")').replace("bootstrap", $theme)); 
     }); 

因此,如果用戶啓動站點時清除所有Cookie,則他們將獲得標準引導主題。在這一點上該網站的網址是http://domain.com/sitename/

如果用戶更改主題現在MyTheme的cookie被保存時,CSS鏈接改變和任何其他頁面,用戶訪問將是正確的選擇的主題。

當用戶返回到主頁上是相同的索引頁面,但由於動作鏈接的URL現在http://domain.com/sitename/Home/Index/我的問題是 - 如果用戶現在改變了主題,索引頁主題更改但導航到任何其他頁面給出了以前的主題。返回到主頁將給出最新選定的主題...現在好像現在有兩個餅乾一個網站寬,一個專門用於家庭/索引

不知道爲什麼會發生這種情況?

爲了完整,這是駐留在_Layout.vbhtml

function getCookie(cname) { 
     var name = cname + "="; 
     var ca = document.cookie.split(';'); 
     for (var i = 0; i < ca.length; i++) { 
      var c = ca[i]; 
      while (c.charAt(0) == ' ') c = c.substring(1); 
      if (c.indexOf(name) == 0) { 
       return c.substring(name.length, c.length); 
      } 
     } 
     return ""; 
    } 

我的getCookie函數任何幫助表示讚賞

回答

0

我的壞...我有在頂級家居鏈接和底部的每一頁和思想我的所有環節回家使用

@Html.ActionLink(" ", "Index", "Home") 

在辦理入住手續時使用的頁面底部的鏈接...

href="~/Home/Index" 

採用了頂級鏈接始終保持網址爲http://domain.com/sitename/,而底部的人給了http://domain.com/sitename/Home/Index

他們都更改爲操作鏈接或href="~/"固定的問題...雖然我仍然不知道爲什麼會是將cookie路徑設置爲/(根)