【问题标题】:The page isn't redirecting properly searched a lot but cant find solution该页面没有正确重定向搜索了很多但找不到解决方案
【发布时间】:2015-02-11 10:25:22
【问题描述】:

我正在尝试一个简单的路由,我的 global.asax 如下所示

 void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    registerroutes(RouteTable.Routes);


}

public static void registerroutes(RouteCollection routecollection)
{
    routecollection.MapPageRoute("home","home/","~/home.aspx");
}

我的主页

 protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {

    Response.Redirect(ResolveUrl("~/home/"));



    }
}

但没有正确重定向不知道我错在哪里

【问题讨论】:

    标签: c# asp.net routing


    【解决方案1】:

    您使用的是 Microsoft 友好 URL Nuget 包吗? (如果没有,我强烈建议)

    如果你使用它,你可以避免在你的主页中使用重定向 -

    您需要在 App_Code>App_Start 中创建一个 RouteConfig.CS,然后添加以下代码(确保您使用的是 System.Web.Routing 和 Microsoft.AspNet.FriendlyUrls

    public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);
    
            routes.MapPageRoute("", "", "~/default.aspx");            
        }
    

    然后在 global.asax 中添加

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
    
    }
    

    这应该可以在您不需要在主页中进行任何类型的重定向的情况下工作。

    【讨论】:

    • 我正在使用 system.web.routing
    • 您在使用 FriendlyURLs Nuget 吗?如果您使用它并按照我的方式设置代码,它肯定会起作用。
    • 我已经找到了第一条路线的解决方案,无需提及response.redirect,因此不会形成路线循环,感谢您的帮助伙计:)
    【解决方案2】:

    请删除 ResolveUrl,因为它不打算在这里使用,它仅用于解析视图 / aspx 代码中的脚本和资源文件。

    Response.Redirect("~/home");

    还有一点,注意C#编码标准,函数名应该是pascal大小写: RegisterRoutes 而不是 registerroutes

    另外,请确保您有一个 home.aspx 页面的物理文件,该文件位于您网站的根目录。

    如果它仍然没有工作,我们需要知道您遇到了什么样的错误以及您是如何运行您的应用程序的?使用 IIS 快递?还是 IIS 服务器?还是经典的开发服务器?

    【讨论】:

    • 仍然没有工作错误是页面没有正确重定向
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2015-04-12
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2016-12-06
    • 2014-08-07
    相关资源
    最近更新 更多