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

别等时光非礼了梦想. 提交于 2019-12-07 20:03:41

问题


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 .


回答1:


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>



回答2:


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




回答3:


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




回答4:


You can use the Uri class

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


来源:https://stackoverflow.com/questions/17399220/how-to-remove-page-name-from-url-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!