我在Umbraco中有两个模板。一个用于桌面,另一个用于移动。我有一个小脚本,它检测请求的用户代理并相应地重定向用户。
如果请求是从桌面发出的,则用户将被重定向到带有URL www.abc.com
的桌面模板。
如果从移动设备发出请求,则用户将被重定向到使用url www.abc.com/?alttemplate=mobilehomepage
的移动模板。
如何使桌面和移动的网址相同.
我使用Response.Redirect
重定向。
提前谢谢。
发布于 2013-04-22 05:37:48
所有的umbraco模板决策都通过default.aspx(.cs)运行,并且可以编程地通过重写PreInit方法来更改模板。
因此,我是如何在default.aspx.cs文件中使用templatenameMobile、templatenameDesktop和templateNameTablet模板实现这一目标的,显然您需要一些方法来说明您是在为移动、平板或桌面服务(您可以从用户代理中推断这些方法):
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
string userAgent = Request.UserAgent;
bool isTablet = IsTablet(userAgent);
bool isMobile = IsMobile(userAgent);
int templateId = umbraco.NodeFactory.Node.GetCurrent().template;
umbraco.template template = new umbraco.template(templateId);
string templateName = StripDevice(template.TemplateAlias);
if (isTablet)
{
Page.MasterPageFile = GetTabletMaster(templateName);
}
else if (isMobile)
{
Page.MasterPageFile = GetMobileMaster(templateName);
}
else
{
Page.MasterPageFile = GetDesktopMaster(templateName);
}
}
public string GetMobileMaster(string templateName)
{
try
{
MasterPage masterPage = new MasterPage();
masterPage.MasterPageFile = string.Format("/masterpages/{0}mobile.master", templateName);
if (masterPage == null)
{
masterPage.MasterPageFile = string.Format("/masterpages/{0}desktop.master", templateName);
}
if (masterPage == null)
{
return Page.MasterPageFile;
}
else
{
return masterPage.MasterPageFile;
}
}
catch (Exception ex)
{
umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, umbraco.BusinessLogic.User.GetUser(0), -1, "Switch template to MOBILE fail " + templateName + " : " + ex.Message);
return Page.MasterPageFile;
}
}
发布于 2013-04-22 02:45:14
您可以尝试使用UrlRewriting。它包括在Umbraco中。试着玩config\UrlRewriting.config
以下是文件:
https://stackoverflow.com/questions/16144629
复制相似问题