更改母版页,如果设备是移动在asp.net web表单应用程序 [英] Change master page if device is mobile in asp.net web form app
问题描述
当你创建一个新的asp.net项目我nvisual工作室2012年,加一个ASCX这个code:`//确定当前视图
变种isMobile = WebFormsFriendlyUrlResolver.IsMobileView(新HttpContextWrapper(上下文));
CurrentView = isMobile? 手机:桌面;
when you create a new asp.net project i nvisual studio 2012 it add an ascx with this code:` // Determine current view var isMobile = WebFormsFriendlyUrlResolver.IsMobileView(new HttpContextWrapper(Context)); CurrentView = isMobile ? "Mobile" : "Desktop";
// Determine alternate view
AlternateView = isMobile ? "Desktop" : "Mobile";
// Create switch URL from the route, e.g. ~/__FriendlyUrls_SwitchView/Mobile?ReturnUrl=/Page
var switchViewRouteName = "AspNet.FriendlyUrls.SwitchView";
var switchViewRoute = RouteTable.Routes[switchViewRouteName];
if (switchViewRoute == null)
{
// Friendly URLs is not enabled or the name of the swith view route is out of sync
this.Visible = false;
return;
}
var url = GetRouteUrl(switchViewRouteName, new { view = AlternateView });
url += "?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);
SwitchUrl = url;`
我真的不uderstand它,它是如何工作的?这是什么奇怪的code?WebFormsFriendlyUrlResolver? i'have现有的项目,我想知道是否有可能切换主页面,如果检测到移动浏览器
I really don't uderstand it, how it works? What is this strange code?WebFormsFriendlyUrlResolver? i'have an existing project and i want to know if it is possible to switch the master page if a mobile browser is detected
推荐答案
WebFormsFriendlyUrlResolver是一个辅助类获取路线关联。如果你想使友好的网址即www.yourdomain.com/myaccount.aspx可以显示为www.yourdomain.com/Account使用
WebFormsFriendlyUrlResolver is a helper class to fetch route association. used if you want to enable friendly urls i.e. www.yourdomain.com/myaccount.aspx can be shown as www.yourdomain.com/Account
您不需要使用这个(为您的特定问题),但它是asp.net的一个很酷的功能,并在RouteTables创建自定义路线变得容易
you dont need to use this (for your specific problem), however it is a cool feature of asp.net and made easy with creating custom routes in the RouteTables
<一个href=\"http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx\">this由Scott文章,帮助我理解了友好的网址
this article by Scott helped me understand friendly URLS
现在您的问题,为移动设备改变母版页,母版页只能在一个页面中的pre-init事件改变。我不知道的另一种方式,因为我相信这是为时已晚。
Now to your problem, change master page for a mobile device, master page can only be changed in pre-init event of a page. I dont know another means to inject a new master page after that as I believe it is too late
当你有很多的网页,这个钩<一个href=\"http://sharepoint.stackexchange.com/questions/21679/how-can-we-use-a-different-masterpage-for-annoymous-and-authenticated-users\">handler到HttpContext的
下面是一个伪code,需要细化你的需求。
when you have many pages, hook this handler to httpcontext below is a pseudo code that needs refining to your needs
void page_PreInit(object sender, EventArgs e)
{
Page p = this.Context.Handler as Page;
if (p != null)
{
// set master page
if(Request.Browser.IsMobileDevice){
p.MasterPageFile = "~/MasterPages/mobile.master";
}
else{
p.MasterPageFile = "~/MasterPages/normal.master";
}
}
}
一旦你想通这一点,确保你SO 读这个解决方案一>其中提到建设母版页为移动设备
Once you have figured this, ensure you read this solution at SO which mentions building master pages for mobile devices
好运气
这篇关于更改母版页,如果设备是移动在asp.net web表单应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!