[英]Maintain state of the object for same browser session
以下是我的ASP.Net Web API控制器代码。 如您所见,这里使用了私有类对象BL,并且实现了Get方法。 对于第一种方法FetchAllDashboardByUserId(int userId),我传递了用户ID,以便可以启动BL对象。 在同一个浏览器会话中,如果调用了第二个get方法,则我不想传递用户ID,因为默认情况下应启动BL,但目前不是这样。 BL对于第二种方法为null,因此我必须向该方法的调用添加用户ID-GetCardDataUI(int userId,int dashBoardID,int cardID)。 我的问题是如何避免这种情况。 我的想法不正确:
我在其中连续调用以下URL的单个打开的浏览器是单个会话:
webapi / ViewR?userId = 1
webapi / ViewR?userId = 1&dashBoardID = 1&cardID = 3
我不想在第二个URL中传递userId。 请注意,如果我将类对象声明为静态对象,则它可以按预期工作,但这不是我想要的,它必须绑定到用户:
public class ViewRController : ApiController
{
// BL object for a user
private static BL accessBL = null;
// HTTP GET for Webapi/ViewR (Webapi - name of API, ViewR - Controller with implementation)
[AcceptVerbs("Get")]
public List<DashboardUI> FetchAllDashboardByUserId(int userId)
{
if (accessBL == null)
accessBL = new BL(userId);
// Use BL object for entity processing
}
[AcceptVerbs("Get")]
public CardDataGetUI GetCardDataUI(int userId, int dashBoardID, int cardID)
{
if (accessBL == null)
accessBL = new BL(userId);
// Use BL object for entity processing
}
}
我希望第二种方法实现是:
[AcceptVerbs("Get")]
public CardDataGetUI GetCardDataUI(int dashBoardID, int cardID)
{
// Use BL class object created in last call for entity processing
// Should not pass userid again
}
您可以轻松地在Session
存储数据:
... first request:
Session["userID"] = userID;
... next request:
int userID = (int)Session["userID"]; // should check for null first, but you get the idea...
但是请记住以下几点:
object
,因此您需要进行类型转换和/或类型检查 null
web.config
)后过期
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.