问题描述:

Im Working in mvc C# project now. i just want to take some values from output cache. because it will reduce round robin in DB.

So i Used [OutputCache(Duration = 1800,VaryByParam = "none")]

when the action method is hitted for the first time, its getting the list values correctly,

Acually i assigned Duration for 30mins. So next request for that Action Method should not strike my DB, it has to give the cached Result.

BUt it lead to an exception, showing "Object reference not set to an instance of an object", which is from the Session

My Code is:

** This is Controller Action **

 [HttpPost]

[OutputCache(Duration = 1800, Location = OutputCacheLocation.Server, VaryByParam = "none")]

public JsonResult GetStateList()

{

Result objResult = new Result();

VRecruitService.StateClient objState = new VRecruitService.StateClient();

using (CandidateModel objModel = new CandidateModel())

{

objResult.Data = objModel.GetStateList().Data;

}

return Json(objResult);

}

and ** This is model Class code, where im getting that exception **

 public static Employee User

{

get

{

object objUser = HttpContext.Current.Session["userDetails"];

// In this above Session only im getting that Exception.

if (objUser is VRecruitService.Employee)

return (VRecruitService.Employee)objUser;

else

return null;

}

set

{

HttpContext.Current.Session["userDetails"] = value;

}

}

Please give me Solution. Thank you.

网友答案:

I believe that since you are caching the output, you need to decorate the controller class with

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]

so that session state is available. Also it seems that an HttpGet verb may make more sense given that you are not POST'ing data to the action.

相关阅读:
Top