+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / HttpModule and RewritePath to sitefinity page

HttpModule and RewritePath to sitefinity page

8 posts, 1 answered
  1. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    04 Aug 2010
    Link to this post
    Hi there..

    I've implemented a HttpModule, and I am trying to RewritePath to an existing page in Sitefinity. Everytime I try this, it says: The resource cannot be found.

    Any ideas ?
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Aug 2010
    Link to this post
    Hi Imran,

    Thank you for using our services.

    Basically your http module should execute before the CmsHttpModule starts executing. This means that your module should be declared befoure the cms module in your web.config. On the other hand have you considered using the built in Sitefinity UrlRewrite module? If your intend only to rewrite paths to Sitefinity pages with your custom module - we have this built in. More information can be found in this webinar: URL Rewriting In Sitefinity.

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    10 Aug 2010
    Link to this post
    Hi Radoslav

    Thanks for those resources. I've been through them but I am still experiencing the same problem. Here is my code:

     

     

    public class BlogRewriter : IHttpModule
    {
        public BlogRewriter() { }
      
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new System.EventHandler(Rewrite_BeginRequest);
        }
      
        public void Rewrite_BeginRequest(object sender, System.EventArgs args)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpContext context = HttpContext.Current;
      
            string sRawURL = context.Request.RawUrl.ToLower();
      
            if (!IsSitefinityException(sRawURL))
            {
      
                if (sRawURL.Contains("/blog/"))
                {
                    context.RewritePath("~/blog/post.aspx?id=2", false);
                }
            }
        }
      
        bool IsSitefinityException(string rawURL)
        {
            rawURL = rawURL.ToLower();
      
            if (rawURL.Contains("sitefinity/admin") && rawURL.Contains("cmspagemode"))
            {
                return true;
            }
            else
            {
                return false;
            }
      
        }
      
        public void Dispose() { }
    }

    my module is defined in the web.config as below:

    <httpModules>
          <add name="BlogRewriter" type="BlogRewriter, App_Code" />
          <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add name="Cms" type="Telerik.Cms.Web.CmsHttpModule, Telerik.Cms"/>
                <!-- Http Module for extension mappings in Library module.-->
                <!--<add name="LibraryModule" type="Telerik.Libraries.LibraryHttpModule, Telerik.Libraries"/>-->
                <add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule, Telerik.Web.UI"/>
            </httpModules>

    Now, in sitefinity there is a page called blog.aspx in the root and also blog/post.aspx

    When I access an example URL like this: http://localhost/mysite/blog/some-page-name-of-some-blog.aspx the page should get rewritten to http://localhost/mysite/blog/post.aspx?id=2 but I keep getting "The resource cannot be found."

    If i navigate to the page http://localhost/mysite/blog/post.aspx?id=2 this works fine.

    Any ideas as to why this isnt working?! All help will be greatly appreciated

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Aug 2010
    Link to this post
    Hello Imran,

    Most probably you are not redirected to the proper url when you use the module. If you attach the code to debugger what is the url that you get when you make response redirect?

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    10 Aug 2010
    Link to this post
    Hi Ivan

    When the code in the module is: 
    context.RewritePath("~/blog/post.aspx?id=2", false);

    the error message is: 
    The Resource cannot be found.
    Description 404
    Requested URL: /mysite/blog/post.aspx

    But when I try: 
    context.Response.Redirect("~/blog/post.aspx?id=2");

    It actually goes to the page - http://localhost/mysite/blog/post.aspx?id=2

    Any ideas?! - thanks
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    11 Aug 2010
    Link to this post
    Hi Imran,

    1. You can use the built-in AdvancedUrlrRewriter  - you could check URL Rewriting section of our User Manual

    2. It looks like RewritePath of the context does not rewrite the path correctly and you get not found exception. You could use Response.Redirect as suggested or the first option which will rewrite the url. Generally, all generic content based modules uses url rewriting and there could be some problem when you tries to rewrite the url with your code.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  7. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    11 Aug 2010
    Link to this post
    Thanks Ivan.

    I ended up just using AdvancedUrlrRewriter and re-wrote my URLs using that.

  8. Chris Aybar
    Chris Aybar avatar
    25 posts
    Registered:
    20 Oct 2008
    03 Nov 2010
    Link to this post
    Just wanted to point out that what you were looking for was solved in this post: http://www.sitefinity.com/devnet/forums/sitefinity-3-x/developing-with-sitefinity/customhttpmodule-rewritepath.aspx
Register for webinar
8 posts, 1 answered