4

from the following url :

http://www.mywebsite.com/default.aspx

I want to remove the default.aspx so the url will look like :

http://www.mywebsite.com/

I need a quick and clean way to do this , and I only need to do this with default.aspx page and no ther pages . thanks in advance .

5
  • my_url = "http://www.mywebsite.com/". Bam! Removed! Commented Jul 1, 2013 at 7:20
  • @Karamafrooz, by default your default page can vary it can be default.html or index.html or default.aspx. So if you just want to remove it from URL you have mentioned dont specify page name and default.aspx opens by default and wont be visible in url as well Commented Jul 1, 2013 at 7:25
  • Do you want to remove default.aspx when user requests for this url mywebsite.com/default.aspx? If user navigate mywebsite.com directly there will not be default.aspx appended. Commented Jul 1, 2013 at 7:36
  • @ Subin Jacob I don't want default.aspx to be showwn on any condition Commented Jul 1, 2013 at 8:10
  • @Karamafrooz, what you need is URL rewriting, for how to do that follow link to my response below else make a google search with "url rewriting asp.net" and you have whole bunch of articles. Commented Jul 1, 2013 at 10:51

4 Answers 4

9

change your webconfig with below code:it solve my same problem.

<?xml version="1.0"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="default.aspx Redirect" stopProcessing="true">
                    <match url="^(.*\/)*default\.aspx$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
                    </conditions>
                    <action type="Redirect" url="{R:1}" redirectType="Permanent"/>
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Sign up to request clarification or add additional context in comments.

Comments

4

you can use Routing

in Global.asax file

     protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);  
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("defualt",
                            "",
                        "~/Default.aspx");
    }

and in your redirection page call

Response.Redirect(GetRouteUrl("defualt", null)); 

for more info read : http://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx

1 Comment

What's the point of calling Response.Redirect(GetRouteUrl("defualt", null));? The first snippet just works.
1

By default your default page can vary it can be default.html or index.html or default.aspx depending on the preference. So if you just want to remove it from URL you have mentioned dont specify page name and default.aspx opens by default and wont be visible in url as well

Whereas if you want to do URL rewriting,there are various ways you can do this, you can make changes in web.config or can have a httpModule of your own have a look at below link:-

http://www.codeproject.com/Articles/2538/URL-Rewriting-with-ASP-NET

Comments

0

You can use the Uri class

var uri = new Uri("http://www.mywebsite.com/default.aspx");
string urlIWant = uri.GetLeftPart(UriPartial.Authority);

3 Comments

If you want to remove the name code side you must follow this. Your question isn't clear! @Karamafrooz
what after urlIWant??, where to place this code and will the above do url rewriting on its own
If you want a url rewrite this is not the solution for you. If you need the url for some other action - use the code above wherever you need in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.