2009-08-26 29 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