Udhay Titus
Udhay Titus

Reputation: 5869

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)

Upvotes: 0

Views: 935

Answers (1)

Sock
Sock

Reputation: 5413

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>

Upvotes: 2

Related Questions