February 19, 2016

Custom pipeline for URL redirect in Sitecore

Sample code snippet

    public class Redirect : HttpRequestProcessor
    {
       
public override void Process(HttpRequestArgs args)
        {
           
try

            {
               
Assert.ArgumentNotNull(args, "args");
              
               
if (Context.Item == null || Context.Database == null || args.Url.ItemPath.Length == 0 || args.Url.FilePath.Contains("/sitecore"
))
                {
                   
return
;
                }
               
if (Context.Database != null
)
                {
                   
string rawURL = string
.Empty;
                    rawURL = Sitecore.
Context
.RawUrl;

                   
// you can write your custom logic here.


                }
            }
           
catch (Exception
ex)
            {

            }

        }

Now you can add this pipeline in "httpRequestBegin" section inside web.config
See sample

<httpRequestBegin>
      <processor type="MyApp.Redirect, MyApp"/>
</httpRequestBegin>

No comments:

Post a Comment