how to remove page name from url in asp.net?

一世执手 提交于 2019-12-06 10:33:48

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>

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

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

You can use the Uri class

var uri = new Uri("http://www.mywebsite.com/default.aspx");
string urlIWant = uri.GetLeftPart(UriPartial.Authority);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!