ASP.NET - 为什么我的cookie没有持久化?

时间:2009-07-06 10:00:26

标签: asp.net-mvc cookies

我正在尝试设置一个cookie来存储选定的部门。该Cookie设置为一个小型表单,其中包含部门的选择下拉列表。这是使用AJAX发布的。

这是我存储cookie的方式:

<AcceptVerbs(HttpVerbs.Post)> _
Function ChangeDepartment(ByVal FormValues As FormCollection) As ActionResult
    If Response.Cookies("department") IsNot Nothing Then
        Response.Cookies("department").Value = FormValues("department")
    Else
        Dim c As New HttpCookie("department")
        c.Value = FormValues("department")
        c.Expires = Now.AddDays(7)
        Response.Cookies.Add(c)
    End If

    Return Json(New With {.newDepartment = Response.Cookies("department").Value})
End Function

使用正确的值正确返回.newDepartment变量。

这是我检索cookie并构建select-dropdown的方法:

<% Ajax.BeginRouteForm("ChangeDepartment", New AjaxOptions With {.LoadingElementId = "loading", .HttpMethod = "post", .OnSuccess = "function(request) {ajaxMessage('Department change', 'Department changed to: ' + request.get_response().get_object().newDepartment);}"})%>
    <select name="department">
        <option>Default</option>
        <option<%If Request.Cookies("department") Isnot Nothing andAlso Request.Cookies("department").Value = "Supervisor" Then Response.Write (" selected=""selected""") %>>Supervisor</option>
        <option<%If Request.Cookies("department") Isnot Nothing andAlso Request.Cookies("department").Value = "Purchasing" Then Response.Write (" selected=""selected""") %>>Purchasing</option>
        <option<%If Request.Cookies("department") Isnot Nothing andAlso Request.Cookies("department").Value = "Engineering" Then Response.Write (" selected=""selected""") %>>Engineering</option>
    </select>
    <input type="submit" value="Change department" />
<% Html.EndForm%>

未存储Cookie,因为select-dropdown会一直返回Default。难道我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用http调试器,如Fiddler for IE和HttpFox for FireFox。 检查cookie是否真的与服务器响应一起发送,以及浏览器是否将其与更多请求一起发回。 从那里你可能会跟踪问题,看看出了什么问题。如果您仍有问题,可以在此处发布http流量。