aav*_*iss 2 c# asp.net umbraco umbraco-blog umbraco5
我在Umbraco有两个模板.一个用于桌面,另一个用于移动 我有一个小脚本,可以检测请求的用户代理并相应地重定向用户.
如果请求是从桌面进行的,则用户将被重定向到带有URL的桌面模板www.abc.com.
如果从移动设备发出请求,则会将用户重定向到带有网址的移动模板 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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1949 次 |
| 最近记录: |