Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net webforms中设置默认页面url路由_C#_Asp.net - Fatal编程技术网

C# 如何在asp.net webforms中设置默认页面url路由

C# 如何在asp.net webforms中设置默认页面url路由,c#,asp.net,C#,Asp.net,我正在尝试在weforms中实现url路由。我做了一部分。你能帮我把路由设置为起始页或索引页吗。在我的路线图课程中,我写了如下内容 routes.MapPageRoute("Index","","~/Index.aspx"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "

我正在尝试在weforms中实现url路由。我做了一部分。你能帮我把路由设置为起始页或索引页吗。在我的路线图课程中,我写了如下内容

routes.MapPageRoute("Index","","~/Index.aspx");
routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
        ); 
我将Index.aspx设置为开始页并运行它。但url仍然是Index.aspx

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
        ); 
在默认页面的MVC中,我们将编写如下所示的行集

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
        ); 
在Webforms中是否有类似的技术??

步骤

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
        ); 
演练:在Web窗体应用程序中使用ASP.NET路由

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
        ); 
已更新

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
        ); 
  • routeName:路由的名称。每个路线必须是唯一的。您可以设置任何唯一的名称,为了更好地理解,我将其命名为客户
  • 路由URL:要实现的路由URL。例如,此处我们希望Customers.aspx仅显示为Customers(不带.aspx扩展名),因此必须在此处定义此自定义
  • physicalFile:路由URL应重定向到的实际ASP.Net页面的URL。对于本例,它是Customers.aspx。 MapPageRoute方法将路由添加到全局路由集合,即RouteTable.Routes。 RegisterRoutes在应用程序启动事件处理程序内调用,以便在应用程序启动时将所有路由添加到RouteTable集合中。 这就是删除或隐藏ASP.Net网页的.ASPX扩展名所需的全部操作,现在,如果您键入URL as或两者都将重定向到客户页面,即customers.ASPX

    routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
            ); 
    
    protected void Application_Start(object sender, EventArgs e)
    {
      RegisterRoutes(RouteTable.Routes);
    }
    
    public static void RegisterRoutes(RouteCollection routes)
    {
      routes.MapPageRoute("Index","Index","~/Index.aspx");
    }
    

  • 我不认为这只能通过使用新的路由技术来实现,但您仍然可以通过在web.config中指定默认页面来实现。这可以通过添加以下内容作为
    节点的子元素来实现(几乎在顶层):

    routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
            ); 
    
    
    
    然而,您的url将显示为
    yourdomain.com
    ,而不是
    yourdomain.com/Index.aspx

    routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
            ); 
    

    另一种选择是让您的
    默认设置。aspx
    页面永久重定向到
    索引

    看看ScottGu@gkrishy routes的一篇非常好的文章RouteCollection@LibinCJacob如果你不使用MVC,你想要的东西可能做不到,因为在webforms中,每个视图/页面都有隐藏的代码,不像Controllers(在MVC中)@manish,实际上路由对我来说是有效的。我的问题是我无法在Webforms中设置默认页面路由。是的,我已经在我的项目中使用了上述代码。不working@LibinCJacob现在检查我的答案。我已经更新了。
    routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
            );