Search code examples
c#asp.netweb-servicesweb-configglobal-asax

How to remove ".aspx" from url in asp.net c#?


I want to remove ".aspx" from my web appliaction url. also I have using webservices.

If I use below code web services is not working.

Global.asax

protected 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");
      }
}

for Eg:-

Actual page is DEFAULT.aspx, but I want to show DEFAULT in address bar. So I used Global.asax to remove (.aspx). It's working fine. but Web service is not working(Default.asmx)


Solution

  • There is a module that will handle this for you without having to directly manipulate the urls, as described here: http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx.


    Install the package, Microsoft.AspNet.FriendlyUrls.

    In your RouteConfig, the extensionless urls are enabled using:

    routes.EnableFriendlyUrls();
    

    You can generate friendly urls using extension methods, for example, to generate /Foo/bar/34, you can use:

    <a href="<%: FriendlyUrl.Href("~/Foo", "bar", 34) %>">Click me</a>