从MVC控制器中的web api读取cookie值集

时间:2016-01-11 13:07:28

标签: asp.net-mvc asp.net-web-api

我遇到了网络Api的问题,并有一个问题:

在MVC控制器中,我无法从Asp.Net Web Api读取cookie值。

Web Api

 [System.Web.Http.HttpPost]
 public System.Net.Http.HttpResponseMessage AddToCart(Interface.Models.CartModel CartModel)
    {
        HttpResponseMessage Response = null;
        try
        {

            // Do some thing

            Response = new HttpResponseMessage(HttpStatusCode.OK);
            var ShoppingCartDetailsCookie = new System.Net.Http.Headers.CookieHeaderValue("CartDetails", "value");
            ShoppingCartDetailsCookie.Domain = Request.RequestUri.Host;
            ShoppingCartDetailsCookie.Path = "/";
            ShoppingCartDetailsCookie.Expires = DateTime.Now.AddMinutes(2);
            Response.Headers.AddCookies(new System.Net.Http.Headers.CookieHeaderValue[] { ShoppingCartDetailsCookie });


            Response.Content = new System.Net.Http.ObjectContent<Invoices>(Cart, new System.Net.Http.Formatting.JsonMediaTypeFormatter());
            return Response;

        }
        catch (Exception ex)
        {
            Log.RaiseError(ClassName, "AddToCart", ex);
            goto ThrowInternalServerError;
        }

    ThrowInternalServerError:
        Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
        Response.Headers.Add("Message", Content.Shared.UnExpectedError);
        return Response;
    }

控制器 公共类SharedController:Controller     {

    public ActionResult ShoppingCart()
    {
        if (Request.Cookies["CartDetails"] != null)
            {
                var StrShoppingCartDetails = Request.Cookies["CartDetails"].Value;
                //Do some thing
            }


        return PartialView("ShoppingCart", ShoppingCart);
    }

}

我从chrome开发人员工具中追踪了Cookie,发现该域名设置为“.domainname”而不是“domainname”。有什么建议吗?

0 个答案:

没有答案