Sitecore context in HttpHandler

2019-06-07 05:53发布

问题:

I have HttpTaskAsyncHandler in my sitecore solution and i call it I have sutup IngnoreUrlPrefix and etc. By some reason i can't get access to SC.Context.Database Database is null in ProcessRequestAsync(HttpContext context) method, it looks like I don't access to Sitecore context in HttpHandler.

How to resolve it ?

Thanks.

回答1:

You wont be able to access Sitecore Context (Database or Item) in the Handler. We have confirmed this with Sitecore Support for our task.

The best way is Implement a Processor in the Request pipeline begin. How to Implement

Inherit HttpRequestProcessor in your class found in (using Sitecore.Pipelines.HttpRequest;)

and add that Processor after SiteResolver in < httpRequestBegin >

<processor type="Sitecore.Pipelines.HttpRequest.SiteResolver, Sitecore.Kernel" />
<!-- Custom Module  -->
<processor type="SND641.Customization.RobotsModule, SND641" />


回答2:

If you choose to ignore your script file (by using IgnoreUrlPrefix), then you it will not be processed by Sitecore's request pipeline and thus will not have a Sitecore context.

I think you can solve it by removing your url prefix from IgnoreUrlPrefix and make sure the file extension of your handler is added to the allowed extensions parameter of the FilterUrlExtensions pipeline processor:

<processor type="Sitecore.Pipelines.HttpRequest.FilterUrlExtensions, Sitecore.Kernel">
  <param desc="Allowed extensions (comma separated)">aspx</param>
</processor>

This way you can call your script and still have Sitecore process all the pipelines.



回答3:

You can get context without pipeline. Along with web.config handler definition, you will need to add under customHandler. It helps in getting sitecore context. E.g

<customHandlers>
    <handler trigger="blogfeed.xml" handler="blogfeed.xml" />
</customHandlers>


回答4:

I'm sorry if I'm not getting exactly what you are trying to do. But by working with handlers I did have issues that my code was not able to access the sitecore object even when I updated the web.config and

I noticed that my handler was like this:

 public class GetHandler: IHttpHandler
    {
      ...
    }

by adding the System.web.SessionState.IrequiresSessionStatem, like this:

  public class GetHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
    {....}

then everything started to work and I was able to get items from sitecore without issues:

Public void ProcessRequest(HttpContext context)
{
   Database webdb = Factory.GetDatabase("web");
}

previous my change the webdb was coming with nothing and the code was coming back with "Command "Sitecore.Database" is not valid"

after that changes, as I said, everything worked for me.

I hope this helps and adds some value to the existing answers.

regards,