Thursday 31 July 2014

Remove .aspx from url in asp.net

Remove or hide .aspx from url in asp.net


In this article I will explain how to remove or hide .aspx from URL in asp.net. Remove or hide .HTML from url in asp.net OR Remove or hide .HTM from url in asp.net. if you want to remove or hide .aspx extension from url in asp.net then
you need to use  URL routing concept in asp.net.

but if you don't want to use URL routing in your code then you can use following code.

The main purpose of this task is make URL as SEO friendly.

Remove html or htm from url using code.

Other Articles - Step by step solution


=> Add Global.asax file in your website or project

=> And add following code in your global.asax file


void Application_BeginRequest(object sender, EventArgs e)
{
    String WebsiteURL = Request.Url.ToString();
    String[] SplitedURL = WebsiteURL.Split('/');
    String[] Temp = SplitedURL[SplitedURL.Length - 1].Split('.');

    // This is for aspx page
    if (!WebsiteURL.Contains(".aspx") && Temp.Length == 1)
    {
        if (!string.IsNullOrEmpty(Temp[0].Trim()))
            Context.RewritePath(Temp[0] + ".aspx");
    }
    /* // This is for HTML page
    if (!WebsiteURL.Contains(".html") && Temp.Length == 1)
    {
        if (!string.IsNullOrEmpty(Temp[0].Trim()))
            Context.RewritePath(Temp[0] + ".html");
    }
    // This is for HTM page
    if (!WebsiteURL.Contains(".htm") && Temp.Length == 1)
    {
        if (!string.IsNullOrEmpty(Temp[0].Trim()))
            Context.RewritePath(Temp[0] + ".htm");
    }*/
 }


=> Copy following div tag in your .aspx page , here you have to use page name only without .aspx extension



<div>
    Remove .aspx from asp.net website
    <a href="Default">Click</a>
    <%--Here write only page name without extension --%>
</div>


=> You can also redirect page without extension


protected void Page_Load(object sender, EventArgs e)
{
        Response.Redirect("Default");
}
 

15 comments:

  1. Thanks sir its working very well

    ReplyDelete
  2. sir i have one problem please resolve .
    if you have time
    actually i am working on asp.net project and i want access my project on any to the any pc using local ip address .my all pc are connected at same lane

    ReplyDelete
    Replies
    1. Thanks for your valuable comment.
      I add one article for your problem , it help you.
      Please check this

      http://problemaspdotnet.blogspot.in/2015/06/how-to-access-aspnet-website-in-lan.html

      Delete
  3. Nice, its working very well. Thanks a lot...

    ReplyDelete
  4. it's working me too, thanks.
    Url for user and seo friendly.

    ReplyDelete
  5. It`s not working got an error on running this above code
    Error: HttpException was unhandled by user code
    Request is not available in this context.
    Error on line : String WebsiteURL = Request.Url.ToString();
    please help me how to resolve it

    ReplyDelete
  6. Error on line : String WebsiteURL = Request.Url.ToString();
    please help me how to resolve it

    ReplyDelete
  7. Error on line : String WebsiteURL = Request.Url.ToString();
    please help me how to resolve it

    ReplyDelete
    Replies
    1. Please try with HttpContext
      like this
      String WebsiteURL = HttpContext.Current.Request.Url.ToString();

      Delete
  8. This is very helpful , It is working like a charm...

    ReplyDelete
  9. Thank you sir, It's working proper.

    Can you please tell me - How to call webservice without adding web reference ?

    Thanks.

    ReplyDelete
    Replies
    1. Thanks for your valuable comment.

      I have added one post for How to call web service without adding web reference, it will help you.
      Please check this


      How to call web service without adding web reference using C#

      Delete
  10. Thank you, Nice

    ReplyDelete
  11. This is very helpful thank you so much sir....

    ReplyDelete
  12. its not working on online. ? any update for online
    only offline working

    ReplyDelete



Asp.net tutorials