【问题标题】:ASP.NET Hide Home Page ExtensionASP.NET 隐藏主页扩展
【发布时间】:2014-04-07 07:07:42
【问题描述】:

我在我的 ASP.NET 4.0 项目中使用 URL 重写来实现 URL 路由。这是来自我的 Global.asax 的示例代码:

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

private void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("main", "Main", "~/Home.aspx");
    routes.MapPageRoute("aboutUs", "About-Us", "~/AboutUs.aspx");
    ...//so on and so forth
}

我还将它添加到 Application_BeginRequest 以将 Home.aspx 转发到同一 Global.asax 中的 Main:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Url.AbsolutePath.Contains("Home.aspx"))
    {
        HttpContext.Current.Response.Redirect("~/Main");
    }
}

这样,任何尝试转到“Home.aspx”的请求都将被路由到“Main”,这仅在第一次加载时发生。路由适用于我项目中的所有页面。

现在,当我将代码发布到我的网站并启动网站时,会出现以下 URL:

www.MyWebSite.com/Main

如何在 URL 的末尾隐藏“Main”并使其看起来像这样:

www.MyWebSite.com

~谢谢

【问题讨论】:

    标签: c# asp.net routing


    【解决方案1】:

    将 Home.aspx 配置为 Web 服务器中的“默认页面”。

    【讨论】:

    • 我这样做了,但由于在 Application_BeginRequest 我重定向到“Main”,它在 URL 中显示 Main。我应该完全删除 Application_BeginRequest 代码吗?
    • 实际上,它成功了!!!!我删除了 BeginRequest 中的重定向并将默认页面设置为 Home.aspx。谢谢!
    • 试试这样的:routes.MapPageRoute("Default", new Route(string.Empty, new RouteHandler("~/Site/Home.aspx")));
    猜你喜欢
    • 2017-09-24
    • 2010-12-18
    • 2012-01-02
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多