在C#MVC3重用跨控制器会话 [英] Reusing session across controllers in C# MVC3

查看:247
本文介绍了在C#MVC3重用跨控制器会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中使用MVC3建立一个网站。该网站是使用web服务的客户端。 Web服务使用每个用户一个唯一的会话ID,这是考虑在每个服务调用的最后一个参数。当您登录时,你得到的会话ID。

I'm creating a website in C# using MVC3. The website is a client that uses a web service. The web service uses a unique session id per user, which is given as the last parameter in each service call. You get the session id when you log in.

我能够获得会话ID,并将其保存在属性中我的用户控制器:

I'm able to get the session id and save it in a property in my user controller:

private string Sid
{
    get
    {
        var sid = Session["Sid"];
        return sid == null ? "" : sid.ToString();
    }
    set
    {
        Session["Sid"] = value;
    }
}

在我的其他控制器我能够得到一个类似的属性(不只是二传手),会话ID,但是当它要求用户控制器做一些地方访问自己的属性来获取ID的会话空。

In my other controllers I'm able to get the session id with a similar property (just without the setter), but when it asks the user controller to do something where it accesses its own property to get the id the session is null.

这似乎是会议没有得到控制器之间传输,但我不知道该怎么做。我想从一个中心位置访问,而不是在每个控制器具有性能会话,但我无法弄清楚如何做到这一点。

It seems like the sessions don't get transferred between the controllers, but I don't know how to do that. I would like to access the session from one central place, instead of having properties in each controller, but I can't figure out how to do it.

在谷歌搜索三天一直没有能够给我答案。你有什么想法?

After three days of searching Google hasn't been able to give me the answer. Do you have any ideas?

推荐答案

为什么不创建一个会在 OnActionExecuting 方法中的Session变量一个基本的控制器,然后让所有的其他控制器从这个基本控制器继承,就像这样:

Why not create a Base Controller which creates the Session variable in the OnActionExecuting method, then make all your other Controllers inherit from this Base Controller, like so:

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        Session["Sid"] = Session.SessionID;
        base.OnActionExecuting(filterContext);
    }        
}

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        ViewBag.Name = Session["Sid"];
        return View();
    }
}

public class TestController : BaseController
{
    public ActionResult Index()
    {
        ViewBag.Name = Session["Sid"];
        return View();
    }
}

希望这有助于!

根据您的评论如下这里,我相信什么,你正在寻找(?):

As per your comment below here is, what I believe, you're looking for (?):

public class BaseController : Controller
{
    public string Sid
    {
        get
        {
            var sid = Session["Sid"];
            return sid == null ? "" : sid.ToString();
        }
        set
        {
            Session["Sid"] = value;
        }
    }
}

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        Sid = "2343432233aaaa";
        ViewBag.Name = Session["Sid"];
        return View();
    }
}

public class TestController : BaseController
{
    public ActionResult Index()
    {
        ViewBag.Name = Session["Sid"];
        return View();
    }
}

您可以替换在那里我将希德与您的服务所产生的SessionID

You can replace where I set Sid with the SessionId generated from your service

这篇关于在C#MVC3重用跨控制器会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆