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

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

问题描述:

我想在我的表单中实现url路由。我已经做了部分。你能帮我把路由设置到初始页面或索引页面吗?在我的路线图类,我写如下如何在asp.net webforms中设置默认页面url路由

routes.MapPageRoute("Index","","~/Index.aspx"); 

,我设置为Index.aspx的作为设定为起始页并运行它。但网址仍然是Index.aspx。

在默认页面的MVC中,我们将编写如下的一组行。

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

在Webforms中有没有像这样的技术?

+0

看看ScottGu的一篇很好的文章[这里](http://weblogs.asp.net/scottgu/url-routing-with-asp-net-4 -web-forms-vs-2010-and-net-4-0-series) – Yogi

+0

@gkrishy路由是Ro的对象uteCollection –

+0

@LibinCJacob如果你不使用MVC,你可能无法做到,因为在webforms中,每个View/Page的后面都有代码,不像控制器(在MVC中) – manish

对于步骤Click Here

演练:在Web使用ASP.NET路由窗体应用程序

你想删除aspx扩展。所以,你应该ST它webconfig这样的:

<rule name="Remove_aspx">    
      <match url="(.*)" /> 
      <conditions logicalGrouping="MatchAll"> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="{R:1}.aspx" /> 
</rule> 
+0

对不起Siamak,这不是我确切需要的。我的需求是如何路由Web表单的第一页。感谢您的回复 –

尝试这种方式,

更新

  1. routeName:路由的名称。每条路线必须是唯一的。您可以设置任何独特的名称,我已将其命名为客户以便更好地理解。
  2. routeUrl:您要实现的路由URL。例如,我们希望Customers.aspx只显示为Customers(不带.ASPX扩展名),因此必须在此处定义此自定义。
  3. physicalFile:路径URL应该重定向到的实际ASP.Net页面的URL。对于这个例子,它是Customers.aspx。 MapPageRoute方法将路由添加到全局路由集合,即RouteTable.Routes。 RegisterRoutes在Application_Start事件处理程序内部调用,以便在应用程序启动时将所有路由添加到RouteTable集合中。 这就是所有您需要删除或隐藏ASP.Net网页的.ASPX扩展名,现在如果您输入的URL为http://localhost:1932/RoutingCS/Customers/http://localhost:1932/RoutingCS/Customers,它们都会将您重定向到客户页面,即Customers.aspx。

    protected void Application_Start(object sender, EventArgs e) 
    { 
        RegisterRoutes(RouteTable.Routes); 
    } 
    
    public static void RegisterRoutes(RouteCollection routes) 
    { 
        routes.MapPageRoute("Index","Index","~/Index.aspx"); 
    } 
    
+0

对不起gkrishy它不工作。 –

+0

是的我已经在我的项目中使用了上面的代码。不工作 –

+0

@LibinCJacob现在查看我的答案。我已经更新了它。 – gkrishy

我不认为这只能通过使用新的路由技术来实现,但你仍然可以在你的web.config中指定的默认页面做到这一点。这可以通过将下面的<configuration>节点(几乎在顶层)的子元素来完成:

<system.webServer> 
    <defaultDocument enabled="true"> 
     <files> 
      <clear /> 
      <add value="Index.aspx" /> 
     </files> 
    </defaultDocument> 
</system.webServer> 

尽管如此,您的网址将显示为yourdomain.com而不是yourdomain.com/Index.aspx

另一种方法是让你的Default.aspx页面做一个永久的重定向到Index