2

从以下网址:

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

我想删除 default.aspx,所以 url 看起来像:

http://www.mywebsite.com/

我需要一种快速而干净的方法来执行此操作,并且我只需要使用 default.aspx 页面而不需要其他页面来执行此操作。提前致谢 。

4

4 回答 4

7

使用以下代码更改您的 webconfig:它解决了我同样的问题。

<?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>
于 2014-02-06T09:30:42.017 回答
4

你可以使用路由

在 Global.asax 文件中

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

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

并在您的重定向页面调用中

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

欲了解更多信息,请阅读: http: //msdn.microsoft.com/en-us/library/cc668201 (v=vs.100).aspx

于 2013-11-18T07:57:40.637 回答
1

默认情况下,您的默认页面可能会有所不同,它可以是 default.html 或 index.html 或 default.aspx,具体取决于首选项。因此,如果您只是想从您提到的 URL 中删除它,请不要指定页面名称,默认情况下会打开 default.aspx,并且也不会在 url 中可见

然而,如果你想进行 URL 重写,有多种方法可以做到这一点,你可以在 web.config 中进行更改,或者可以拥有自己的 httpModule 看看下面的链接:-

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

于 2013-07-01T07:22:40.497 回答
0

您可以使用Uri

var uri = new Uri("http://www.mywebsite.com/default.aspx");
string urlIWant = uri.GetLeftPart(UriPartial.Authority);
于 2013-07-01T07:34:13.847 回答