2015-07-21 115 views
2

我想在我的表單中實現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

回答

0

對於步驟Click Here

演練:在Web使用ASP.NET路由窗體應用程序

0

你想刪除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表單的第一頁。感謝您的回覆 –

2

嘗試這種方式,

更新

  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

0

我不認爲這隻能通過使用新的路由技術來實現,但你仍然可以在你的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