【发布时间】:2014-06-06 09:24:44
【问题描述】:
我正在尝试使用 Nancyfx 自托管。问题是当我在Get["path"] 中设置Session["key"] = value,然后我在Post["path"] 中调用它时,我得到空,反之亦然。下面是我的代码
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
CookieBasedSessions.Enable(pipelines);
}
}
public class TestModule : NancyModule
{
public TestModule()
{
Get["/"] = _ =>
{
Session["App1"] = "Ola";
return Session["App1"] + " " + "Hello World";//"Ola Hello World"
};
Get["/about"] = _ =>
{
return Session["App1"];//"Ola Hello World"
};
Post["/create"] = _ =>
{
return Session["App1"];//emtpty
};
Post["/add"] = _ =>
{
return Session["App1"];//empty
};
}
}
class Program
{
static void Main(string[] args)
{
var cfg = new HostConfiguration();
cfg.UrlReservations.CreateAutomatically = true;
var host = new NancyHost(new Bootstrapper(), cfg, new Uri("http://localhost:5050"));
host.Start();
Console.ReadKey();
WebRequest wr1 = HttpWebRequest.Create("http://localhost:5050/create");
wr1.Method = "POST";
wr1.GetRequestStream();
StreamReader sr1 = new StreamReader(wr1.GetResponse().GetResponseStream());
Console.WriteLine(sr1.ReadToEnd());
Console.ReadKey();
WebRequest wr2 = HttpWebRequest.Create("http://localhost:5050/add");
wr2.Method = "POST";
wr2.GetRequestStream();
StreamReader sr2 = new StreamReader(wr2.GetResponse().GetResponseStream());
Console.WriteLine(sr2.ReadToEnd());
Console.ReadKey();
host.Stop();
}
}
由于这个问题,现在我无法保存登录状态或一些必要的信息。你们有解决办法吗?
【问题讨论】:
-
Cookie 不会重新附加到您发出的每个请求。按预期工作。