I want to destroy the session when the user clicks the fa-fa-close icon.

I tried creating an ajax call and destroying the session.

But when I call the ajax method this method throws an error.

Can anyone help why this has happened?

enter image description here

enter image description here

  • 1- Never user /api/sitecore 2- RenderingContext will be null in ajax request
    – Abhay Dhar
    Jan 30, 2020 at 9:33
  • Then How I call my controller back ?
    – Rajasekar
    Jan 30, 2020 at 9:54
  • There is no need to use renderingcontext ,you can use Sitecore.Contexxt.Item or GetItem , also configure a seperate route , dont use /api/sitecore , it will break in your CD server
    – Abhay Dhar
    Jan 31, 2020 at 5:44
  • Instead of screenshot's, can you paste your code, and error messages as code blocks? This will allow others to search on similar keywords to find this post. Search can't read text in images. Nov 16, 2022 at 16:05

4 Answers 4


Sitecore.Context.Item would be null when you will call a method through ajax call in the view as per my understanding. You need to provide Sitecore.Context.Item at the view level(.cshtml file). If you need item id in the method then you can pass the code below in the parameters in ajax call and get it on the method and get the item based on pass item id.


I had the same problem Sitecore.Context.Item value was null when calling through ajax call.

Hope it helps !!


Use RenderingContext.CurrentOrNull if you need to test for the existence of the rendering context rather than RenderingContext.Current. If you have no current rendering context (ie an ajax call) then attempts to read RenderingContext.Current throw an exception. I know you're doing a null check on it, but if it is null it will throw an exception when reading it, just one of those Sitecore oddities.


Based on your error, the controller is getting called but since the RenderingContext is null the error is being thrown. The RenderingContext is always null in AJAX call.


public Item ContextItem = Sitecore.Context.Item;

That should solve your issue. Let me know in case you need any further help.


If you want to clear the session, use the following generic method and destroy the value from the session.

public static void Remove(string key)
            var session = HttpContext.Current?.Session;
            if (null != session && session[key] != null)
                session[key] = null;

Sitecore.Context.Item and RenderingContext will always be null when you try to invoke from the API context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.