Do not show a file with a particular extension when the website is running in IIS

Last post 12-03-2007 4:37 AM by Guang-Ming Bian - MSFT. 2 replies.

Sort Posts:

  • Do not show a file with a particular extension when the website is running in IIS

    11-28-2007, 6:45 AM
    • Member
      point Member
    • ankushlalit
    • Member since 11-23-2007, 4:40 AM
    • Posts 8

    Hi, 

    How to stop a website from opening a particular file in the view mode. The problem is that I have few files with extension (.default) like navigator.cfg.default and web.config.default and when I run the web application then it is possible to view those files. How can I stop it?

    I tried the following code:

    namespace HttpUrlRewrite 
    {
        /// <summary>
        /// Summary description for HttpUrlRewrite
        /// </summary>
        public class Rewriter  : System.Web.IHttpModule
        {
    
            public void Init(System.Web.HttpApplication Appl)
            {
                Appl.BeginRequest += new System.EventHandler(Rewrite_BeginRequest);
            }
    
            public void Dispose()
            {
            }
    
            public void Rewrite_BeginRequest(object sender, System.EventArgs args)
            {
                System.Web.HttpApplication App = (System.Web.HttpApplication)sender;
                string path = App.Request.Path;
    
                string strExt = System.IO.Path.GetExtension(path);
                if (strExt == ".default")
                {
                    //Do Something
                    HttpContext.Current.Response.Redirect("default.aspx");
                }
                else
                {
                    //Do Something
                }
    
            }
        }
    }
    

     

    		<httpModules>
    			<add type="HttpUrlRewrite.Rewriter" name="HttpUrlRewrite" />
    		</httpModules>
    

    But the code didn't work under the IIS. However when I run the project from Visual Studio and try to open a file with extension (.default), I am redirected to default.aspx but when I try to open the .defualt file from the IIS, I am still able to open the file.

    I have one more question here - Is there any performance issue if I am redirecting the user to a different page with HttpModule every time he enters a particular extension? 

    Thanks..

  • Re: Do not show a file with a particular extension when the website is running in IIS

    12-03-2007, 4:23 AM
    Answer

     Hi,
    The problem is most likely to be the association of the ".default" extension in IIS.
    If this is not set up to go through the aspnet_isapi.dll (through the ASP.NET "engine") then your HttpModule will obviously never catch it.
    You will need to configure IIS in this manner for your module to work.

    In development it will be different if you are debugging through VS2005... this does not use IIS to run the project - it uses an "ASP.NET development server" - and therefore the ".default" extension will be picked up by the HttpModule.

    Regards,
    foreachbiscuit
    blog @ http://foreachbiscuit.wordpress.com
  • Re: Do not show a file with a particular extension when the website is running in IIS

    12-03-2007, 4:37 AM
    Answer

    Hi ankushlalit ,

    I have your code, it works fine. I tried the following steps:

    1. Create a web project
    2. Create a HttpModule project
    3. Create a HttpHandle project
    4. Add the following config to web.config file
      <httpModules>
         <add type="MyHttpModuleTest.Rewrite" name="MyHttpModuleTest"/>
        </httpModules>
          <httpHandlers>
            <add verb="*" path="*.default" type="MyHttpHandle.DefaultExtension,MyHttpHandle"/>
          </httpHandlers>
    5. Config IIS to handle default extension
    6. I have a web page, with following context
          <form id="form1" runat="server">
          <div>
          <a href="1.default">default show</a>
          </div>
          </form>
    7. When I click the "default show" link, it will redirect to default.aspx file.

    If you still can't solve your question, can you please explain the following question:

    But the code didn't work under the IIS. However when I run the project from Visual Studio and try to open a file with extension (.default), I am redirected to default.aspx

    • But the code didn't work under the IIS, can you explain what's "code didn't work under the IIS" , what's "However when I run the project from Visual Studio and try to open a file with extension (.default), I am redirected to default.aspx ", what's difference ?

    Best regards,
    Guang-Ming Bian - MSFT

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (3 items)