URL Rewriting: exchange the file extension

Discussion in 'General troubleshooting' started by peemuzik, Feb 15, 2011.

  1. Hello guys,

    Have a file "__captcha.aspx", which generates a captcha jpeg by
    Code:
    <img src="__captcha.captcha" />
    I'm using that simple code to URL Rewrite in Application_BeginRequest in Global.asax:
    Code:
    string parts = HttpContext.Current.Request.Path.ToLower();
            if (parts.Contains(".captcha")) { Context.RewritePath(parts.Replace(".captcha", ".aspx")); }
    So, the problem is: on my local server works great, on Winhost - ERROR404. I'm upset :( how to solve?
     
    Last edited by a moderator: Oct 14, 2015
  2. Ray

    Ray

    Do you have a URL for us to look at where we can see the full error message?
     
  3. Hello Ray,

    Let me show you this way:

    Code:
    HTTP Error 404.0 - Not Found
    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
    
    Module	IIS Web Core
    Notification	MapRequestHandler
    Handler	StaticFile
    Error Code	0x80070002
    
    Requested URL	http://somewebsite.org:80/__captcha.captcha
    Physical Path	E:\web\somewebsite\__captcha.captcha
    Logon Method	Anonymous
    Logon User	Anonymous
    May be it needs to restart an Application to refresh and enable Global.asax Application_BeginRequest?
     
  4. Ray

    Ray

    The 404 error is fairly straight forward and it does mean it cannot find an object or a file. Make sure the source file is coded to the correct path. I've also seen this when an assembly may not be installed on the server. Because of security we cannot install any 3rd party components directly on the server so make sure that what ever assembly your application is using is bin deployable. Try uploading the assemblies in your Bin folder. The fact that it works on your computer does let us believe that you have something in your servers GAC that we do not have installed. Like I said hopefully it is bin deployable. Also just as a test also set the MIME type for .captcha. You can do this in IIS 7 Manager under the MIME type module.

    http://support.Winhost.com/KB/a628/using-the-microsoft-iis-70-manager.aspx
     
    Last edited by a moderator: Oct 14, 2015
  5. If I use .aspx extension, everything's going great! And, as far as I understand, I'm not using no any components to rewrite the url, I just use C# code inside of Global.asax (even do not edit web.config/machine.config file)

    P.S.: It's a panic))), I don't understand, why that is going on: it is so easy to replace some text in a string, but doesn't work on Winhost server(((
    Most of all I think "Application_BeginRequest" did not enabled/started/refreshed/etc., so Server don't even know that I inserted the rewriting code inside of it.
     
    Last edited by a moderator: Oct 14, 2015
  6. Ray

    Ray

    Did you try setting the MIME type. Basically to tell IIS to treat .captcha like .aspx?
     
  7. That is different solution ("for a black day" haha))), it's good if I have a real "captcha.captcha" file, but now I have a normal "captcha.aspx", just need to rewrite all incoming requests contains ".captcha" to the real ".aspx" file.

    P.S.: Ray, I'm so sorry, disturbing you whole day ^^
     
  8. Ray

    Ray

    I don't quite understand your posting response..
     
  9. Ray, I'll try to make it clear (sorry if repeated again):

    Have
    I have a real __captcha.aspx file, which generates a "image/jpeg" stream

    Need
    Need to make a virtual URL rewriting like <img src="__captcha.captcha" /> which will touch the real .aspx file, not .captcha

    Now
    Now server tries to open __captcha.captcha file instead of looking thru URL rewriting code.

    MIME type - created,
    Application_BeginRequest - created

    Just checked again on my server:
    Code:
        protected void Application_BeginRequest(object sender, EventArgs e)
        {      
            string parts = HttpContext.Current.Request.Path.ToLower();
            if (parts.Contains(".captcha")) 
            { 
                Context.RewritePath(parts.Replace(".captcha", ".aspx"));
            }
        }
    Working the best way (((
     
  10. Ray

    Ray

    I really do not have enough information to troubleshoot this. It would help if I have instructions on how to actually replicate the error message. If you don't want to post your URL on this forum then email it to me and send me instructions on how to replicate the error message.
     
  11. Guys! Fixed! Hurrrayyy!

    Application_BeginRequest was really omitted by Winhost configuration by default. BeginRequest runs only for *.aspx files (for sure "*.captcha" extension is not an active server page by server's opinion)

    I've been just added "*.captcha" extension into Handler Mappings related to System.Web.UI.PageHandlerFactory and it starts!

    Hope it'll be useful for guys, who need use own extensions on a website.

    P.S.: Ray, thanks for idea about MIME, I started from there ;)
     
    Last edited by a moderator: Oct 14, 2015
  12. Ray

    Ray

    Thanks for the explanation. Actually I saw something similar to this and we went through a series of steps and the last one was actually the MIME configuration. It worked after that. Although I really was not sure why that would work. Your explanation shed more light to it and the next time I point to that direction I can explain further why it is needed.
     

Share This Page