2011-03-01 68 views
1

我需要將用戶從一個控制器重定向到其他控制器。使用return RedirectToAction錯誤重定向

我使用

return RedirectToAction("Index", "Project"); 

這偉大的工作,除非我發表我的web.My網在IIS目錄中運行,但是URL看起來像這樣

http://localhost/Project/index 

,但它應該是右

http://localhost/webapp/Project/index 

編輯

Is there a "/" in beginning of your routing?是什麼意思?

是的,該目錄設置爲IIS應用程序。

沒有什麼特別,但在這裏它是:

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
      ); 
     } 
+2

這是不可能的。你必須從你的描述中遺漏一些東西。 'RedirectToAction'工作得很好,並考慮到你的應用程序部署在虛擬目錄下。所以錯誤的JavaScript,錯誤的路由,在其他地方硬編碼的網址,在IIS中錯誤地部署應用程序......?可能性是多重的,但絕對不是'RedirectToAction'。 – 2011-03-01 21:42:47

+0

它可能被錯誤地部署在IIS中的應用程序。我在wwwroot目錄中創建了一個目錄,並將其設置爲一個IIS應用程序(.NET 4集成池)。然後我使用Visual Studio中的發佈選項並將其指向此目錄(文件系統路徑)。 – user256034 2011-03-02 08:12:47

回答

0
  • 是否有一個「/」在你的路由的開始?
  • 您是否確定虛擬目錄在IIS中設置爲應用程序?

你的路由規則添加到這個問題,以幫助您更多的(然後請在答題時,你讓我知道你會做評論,謝謝).T

+0

我發佈了我的路由規則。 – user256034 2011-03-02 08:09:32

+0

您的路線看起來正確,您是否嘗試發佈到IIS而不是文件系統? – Meligy 2011-03-02 21:53:00

0

變更路線:

routes.MapRoute(
       "Default", // Route name 
       "webapp/{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
      ); 
1

如果你想從

http://localhost/Project/index 

的路徑更改爲

http://localhost/webapp/Project/index 

然後把前綴路線註冊,如:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "Default", // Route name 
      "webapp/{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 
    }