2010-08-18 27 views
2

我在我的asp.net webforms 3.5sp1項目上安裝了webform路由設置。我希望在包含主頁的內容目錄中包含該站點的文件,因爲我想使用同一個系統運行多個站點。Webform路由主頁在像mvc而不是根目錄的文件夾中

在MVC中有一個空白的默認頁面,主頁位於一個名爲home的文件夾中。我似乎無法複製這種行爲使用web表單路由,但希望。空白頁總是首先被擊中。路由處理程序第二次被觸發 - 它認識到請求是用於主頁並設置了路由頁面但未被使用。路線處理程序代碼很簡單:

public string VirtualPath { get; private set; } 

    public IHttpHandler GetHttpHandler(RequestContext 
      requestContext) 
    { 
     string file = requestContext.RouteData.GetRequiredString("File"); 
     string id = requestContext.RouteData.GetRequiredString("Id"); 
     string queryString = "?menuid=" + id; 
     VirtualPath = "~/" + file; 
     HttpContext.Current.RewritePath(
      string.Concat(
      VirtualPath, 
      queryString)); 

     var page = BuildManager.CreateInstanceFromVirtualPath 
      (VirtualPath, typeof(Page)) as IHttpHandler; 
     return page; 
    } 

有沒有反正我可以做到這一點?

更新 這裏是我的Global.asax航線代碼:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     Domain.RepositoryFactory repo = new RepositoryFactory(); 
     foreach (var x in repo.MenuRepository.GetAllEnabledGetMenus()) 
     { 
      if (string.IsNullOrEmpty(x.Url)) 
      { 
       //add default 
       System.Web.Routing.RouteTable.Routes.Add(
        new Route("Default.aspx", 
         new RouteValueDictionary(new { File = x.FileName, 
                 Id = x.Id.ToString() }), 
           new CoreRouteHandler())); 
      } 
      else 
      { 
       string url = x.Url; 
       if(x.Url.StartsWith("/")) 
       { 
        url = url.Remove(0, 1); 
       } 
       System.Web.Routing.RouteTable.Routes.Add(
        new System.Web.Routing.Route(url, 
         new RouteValueDictionary(new {File = x.FileName, 
                 Id = x.Id.ToString()}), 
          new CoreRouteHandler())); 
      } 

     } 

    } 
+0

不要以爲你需要虛擬路徑的'〜'。請說明你如何在Global.asax中設置你的路線?路由處理程序應始終首先被擊中! – Markive 2010-08-18 10:18:22

+0

我已添加全局代碼。當我一步一步通過default.aspx頁面加載時,首先命中路由處理程序。所有其他頁面的路線很好。 – 2010-08-18 11:47:04

+0

如果我刪除〜我得到一個錯誤在這裏不允許相對虛擬路徑[x]。 – 2010-08-18 11:49:06

回答

0

好吧我得到了它的工作位!時刻。

我發現這個鏈接,而谷歌搜索:

http://blog.ysatech.com/post/2010/07/11/ASP-NET-4-URL-Routing-Default-Page.aspx

所有我需要做的就是離開航線空白,並從網站的根目錄中刪除Default.aspx文件。

我也複製了默認的system.webserver之二從一個新的MVC項目,因爲我認爲我在那裏是不對的。我正在升級一箇舊項目,因此認爲它沒有配置爲100%。特別是:失蹤。

0

在我的項目,我需要像重定向到www.site.com/MyPage所有/Pages/MyPage.aspx來電。通過使用HttpModule完成。示例代碼如下:

public void Init(HttpApplication context) 
    { 
     context.BeginRequest += new EventHandler(context_BeginRequest); 
    } 

    void context_BeginRequest(object sender, EventArgs e) 
    { 
     HttpApplication app = sender as HttpApplication; 

     if (app.Context == null || app.Context.Response == null) 
      return; 

     String sourceUrl = String.Empty; 
     try 
     { 
      sourceUrl = app.Request.FilePath.TrimStart('/').TrimEnd('/').ToLower(); 
      if (global::MyProject.Global.UrlShortcuts.ContainsKey(sourceUrl)) 
      { 
       String newUrl = global::MyProject.Global.UrlShortcuts[sourceUrl]; 
       app.Context.RewritePath(newUrl, string.Empty, app.Request.QueryString.ToString(), false); 
           } 
      else 
      { 
      } 
     } 
     catch (Exception Ex) 
     { 
      // handle your exception here 
     } 
    } 

小問題與hoster的IIS,因爲我無法配置它來處理所有使用ASP.NET的請求。因此,如果在IIS下運行,則必須爲應用程序啓動時動態創建的頁面提供空白佔位符.aspx文件(例如www.site.com/MyPage/default.aspx)。

String server = Context.Request.ServerVariables["SERVER_SOFTWARE"]; 
// IIS puts some stuff here, WebDev server leaves the field empty 
+0

感謝您的回答,我可以這樣做,但它不使用我想使用的webform路由。 – 2010-08-30 13:52:14

相關問題