locked
Remove filename extension in c# RRS feed

  • Question

  • User-1888080761 posted

    Hi,

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

    http://localhost:60527/WebSite1test/Default.aspx

    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

Answers

All replies

  • User1633621018 posted

    Hi,

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

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

    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. 

    Regards

    Pawan

    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?

    Regards,

    Ali Raza

    Tuesday, September 23, 2014 7:16 AM