2009-08-26 27 views
1

說URL的部分我有3個版本的網站:A,B和C.我想我的網址,以這樣的形式:如何自動包括與Asp.Net MVC

{siteType}/{controller}/{action}/{id} 

其中siteType等於a,bc

當用戶在網站的A版本中時,他們應該呆在那裏;因此所有生成的網址都應具有a的siteType。同樣,對於B和C.

我不想在生成URL時明確指定siteType - 我希望它自動生成。此外,siteType參數將永遠只用於一個地方 - 在基本控制器類中被忽略的RenderView方法 - 它將使用siteType參數爲該版本的網站挑選出正確的視圖,CSS等。因此我對siteType沒有興趣作爲我的行爲的一個論據。只有RenderView方法需要訪問它。

達到此目的的最佳方法是什麼?

回答

1

我們幾乎與網站的語言(段從我們的global.asax.cs)相同:

routes.MapRoute(
      "DefaultLanguage", 
      "{lang}/{controller}/{action}/{id}", 
      new { lang = "de-CH", controller = "Home", action = "Index", id = "" } 
     ); 

只要沒有語言設置的語言將被設置爲SWISS-德國人在我們的情況。

任何操作鏈接都會從當前網站自動獲取語言代碼。所以我們不必在動作鏈接上指定任何語言。

更改siteType很簡單,只需將routeValues添加到您的操作鏈接即可。例如

<%= Html.ActionLink("Linktext", "Action", "Controller", new { siteType = "b" } %> 
+0

是的,但重點是我不希望將siteType添加到每個ActionLink或生成的任何其他URL。由於它在任何地方完成,我應該能夠自動添加到一個地方。 – darasd 2009-08-27 08:27:20

+0

您不必通過「手動」將siteType添加到每個網址。在默認例程中設置一次,它將被添加。如果您想在siteType之間切換,那麼您必須將siteType添加到該操作鏈接。 我的所有鏈接都是這樣的:<%= Html.ActionLink(「Linktext」,「Action」,「Controller」%>除了切換語言的鏈接之外,動態鏈接呈現鏈接並自動添加siteType爲: Linktext griti 2009-08-27 08:56:40

+0

你知道嗎,我認爲你是對的!它似乎是自動添加的,如果沒有明確添加到例如ActionLink中,它會自動添加當前url的值而不是默認值。 – darasd 2009-08-27 10:15:31

0

我可能是相當關閉基地,但它聽起來像是對我來說是一個移動網站一拉

http://weblogs.asp.net/mschwarz/archive/2007/11/26/mvc-framework-and-mobile-device.aspx

公共類 MobileCapableWebFormViewEngine: WebFormViewEngine { 公衆覆蓋ViewEngineResult FindView(ControllerContext controllerContext,string viewName, string masterName,bool useCache) { ViewEngineResult result = null; var request = controllerContext.HttpContext.Request;

// Avoid unnecessary checks if this device isn't suspected to be a mobile device 
    if (request.Browser.IsMobileDevice) 
    { 
     result = base.FindView(controllerContext, "A/" + viewName, masterName, useCache); 
    } 

    //Fall back to desktop view if no other view has been selected 
    if (result == null || result.View == null) 
    { 
     result = base.FindView(controllerContext, viewName, masterName, useCache); 
    } 

    return result; 
} } 

你也代替

Request.Browser.IsMobileDevice


Request.ServerVariables [ 「URL」。載有( 「A」)

+0

是的,這解決了獲取正確視圖的問題,但它不能解決將siteType(「a」)自動添加到每個url的問題。 – darasd 2009-08-27 09:10:34