Hide Extension in Website with Asp.net

Steps:

1: First Create a Global.asax file in your website
2: Paste the code below in Global.asax file .


<%@ Application Language="C#" %>

<script runat="server">

    void Application_BeginRequest(object sender, EventArgs e)
    {
        string curruntpath = Request.Path.ToLower();


        curruntpath = curruntpath.Trim();
        bool isredirected = curruntpath.EndsWith(".aspx");
        bool isredirected2 = curruntpath.EndsWith(".html");
        bool isredirected3 = curruntpath.EndsWith(".jpg");
        bool isredirected4 = curruntpath.EndsWith(".jpeg");
        bool isredirected5 = curruntpath.EndsWith(".gif");
        bool isredirected6 = curruntpath.EndsWith(".png");
        bool isredirected7 = curruntpath.EndsWith("css");
        bool isredirected8 = curruntpath.EndsWith(".js");
        bool isredirected9 = curruntpath.EndsWith(".png");
        //bool isredirected7 = curruntpath.EndsWith(".pdf");
        // string atr = curruntpath.Substring(curruntpath.IndexOf("/"));
        //if (!isredirected && !isredirected2 && !isredirected3 && !isredirected4 && !isredirected5 && !isredirected6)
        //{
        //    HttpContext obj = HttpContext.Current;
        //    string finalurl = curruntpath + ".aspx";
        //    obj.RewritePath(finalurl);
        //}
        string strURL = Request.RawUrl.ToLower();
        bool ok = strURL.Contains(".");
        if (ok == false)
        {
            if (isredirected != true)
            {
                HttpContext obj = HttpContext.Current;
                string finalurl = curruntpath + ".aspx";
                obj.RewritePath(finalurl);
            }
        }

        //&& isredirected2 == true && isredirected3 == true && isredirected4 == true && isredirected5 == true && isredirected6 == true && isredirected7 == true && isredirected8 == true && isredirected9 == true)
    }

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup



    }

    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }
     
     
</script>

3: Change Web.config file with below script.

<validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"></modules>

4:Browse the page without extension .

Ex(http://localhost/test/default.aspx  instead browse (http://localhost/test/default )

If you have any doubt or problem please feel free to mail to my id ( smruti.writeme@gmail.com ) or  contact with my facebook page ( https://www.facebook.com/MicrosfotTechnologyGiants)

Thanks
Smruti
free counters

Comments

Popular posts from this blog

Convert HtmlToPDF in ASP.net & iTextshrap

Create Pivot Table In C#

How to Display Page Wise Total Amount & Grand Total Amount in Last page SSRS & RDLC Report