Remove filename extension in c# RRS feed

  • Question

  • User-1888080761 posted


    I have an address like this and I need to remove certain part from the URL


    I need to remove Default.aspx from the URL and how can I achieve this?

    I tried this code and it doesn't work

    String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery.ToString(), "/");

    Really appreciate any help on this.

    Friday, September 19, 2014 1:13 AM


All replies

  • User1633621018 posted


    If can use Url rewriting to hide the extensions. A simple example would be:

                <rule name="RewriteASPX">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <action type="Rewrite" url="{R:1}.aspx" />

    Please ensure that you are using IIS version 7 or above as rewrite module was not available in earlier versions. Also please ensure that IIS rewrite module is installed from Control Panel >> Programs. 



    Friday, September 19, 2014 1:34 AM
  • User-434868552 posted

    @vignesht5  you question is unclear ... so perhaps you could tell us WHY you wish to do this.

    are you trying to have friendly URLs in WebForms?

    if you are, you're in luck because Peter Shaw recently posted this:  http://www.codeguru.com/columns/dotnet/creating-webforms-with-friendly-urls.html

    "Creating Webforms with Friendly URLs"

    if not, please clarify.

    you may find this article useful:  http://weblogs.asp.net/gerrylowry/clarity-is-important-both-in-question-and-in-answer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 19, 2014 7:44 AM
  • User1428246847 posted

    You can use the LastIndexOf method. The below strips the extension (including the dot) from the string strFilename.

    strFilename = strFilename.Substring(0, strFilename.LastIndexOf('.'));

    You can easily modify this to strip of the complete Default.aspx from your strUrl by replacing the '.' by '/'.

    Sunday, September 21, 2014 5:54 AM
  • User216739479 posted

    Hi there,

    What are you trying to do basically? Are you trying to do URL rewriting or using it for other purpose?


    Ali Raza

    Tuesday, September 23, 2014 7:16 AM