ASP .NET Web API如何从mvc控制器

时间:2018-03-21 10:55:21

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

我的Api控制器

public class UserController : ApiController
{
    public HttpResponseMessage Get()
    {
        var response = new HttpResponseMessage();
        var valueSession = new NameValueCollection();

        valueSession.Add("user_id", 1);
        CookieHeaderValue cookie = new CookieHeaderValue("session", valueSession);
        cookie.Expires = DateTimeOffset.Now.AddDays(2);
        cookie.Domain = Request.RequestUri.Host;
        cookie.Path = "/";

        response.Headers.AddCookies(new CookieHeaderValue[] { cookie });

        return response;
    }
}

我的控制器 - 我需要检索我从API设置到此控制器的cookie。有什么建议吗?

public class UserController : Controller
{
    // GET: User
    public ActionResult Login()
    {
        return View();
        //do something
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

public ActionResult Login()
{
    var userIdVal = Request.Cookies["session"].Values["user_id"];
    return View();
    //do something
}