[英]Page redirect without changing the URL-Umbraco | C#
我在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;
}
}
您可以嘗試使用UrlRewriting。 它被包含在Umbraco中。 嘗試玩config \\ UrlRewriting.config
這里是文檔:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.