我在 Umbraco 中有两个模板。一个用于台式机,另一个用于移动设备。我有一个小脚本,它检测请求的用户代理并相应地重定向用户。
如果请求是从桌面发出的,则用户将被重定向到带有 URL 的桌面模板www.abc.com
。
如果从移动设备发出请求,用户将被重定向到带有 url 的移动模板www.abc.com/?alttemplate=mobilehomepage
如何使桌面和移动设备的 URL 相同。
我正在使用Response.Redirect
重定向。
提前致谢。
我在 Umbraco 中有两个模板。一个用于台式机,另一个用于移动设备。我有一个小脚本,它检测请求的用户代理并相应地重定向用户。
如果请求是从桌面发出的,则用户将被重定向到带有 URL 的桌面模板www.abc.com
。
如果从移动设备发出请求,用户将被重定向到带有 url 的移动模板www.abc.com/?alttemplate=mobilehomepage
如何使桌面和移动设备的 URL 相同。
我正在使用Response.Redirect
重定向。
提前致谢。
所有 umbraco 模板决策都通过 default.aspx(.cs) 运行,您可以通过重写 Page 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;
}
}
您可以尝试使用 UrlRewriting。它包含在 Umbraco 中。尝试使用 config\UrlRewriting.config
这是文档: