ASP.NET关闭FriendlyURLs mobile.master页面

Pio*_*ski 18 asp.net friendly-url

我想完全关闭Site.Mobile.Master页面.我的网站是响应式的,我希望移动浏览器使用相同的母版页.

我如何在ASP.NET友好的URL中执行此操作

谢谢

Lev*_*evi 14

删除Site.Mobile.Master页面,友好URL将只使用常规的Site.Master页面.


Mik*_*SFT 11

实际上,当前版本的Web Forms友好URL(1.0.2)中似乎存在一个错误,该错误使得尝试访问友好URL代码中的site.mobile.masterbreak "The relative virtual path 'Site.Mobile.Master' is not allowed here.".我被这个烧了.

为了解决这个问题,我在http://www.davidwilhelmsson.com/disabling-mobile-master-pages-with-asp-net-friendly-urls/上使用了修改后的代码版本- 首先制作了一个解析器类:

/// <summary>
/// This is a hack to force no mobile URL resolution in FriendlyUrls.  There's some kind of bug in the current version that
/// causes it to do an internal failed resolve of a mobile master even though there is none.
/// </summary>
public class BugFixFriendlyUrlResolver: Microsoft.AspNet.FriendlyUrls.Resolvers.WebFormsFriendlyUrlResolver {
    protected override bool TrySetMobileMasterPage(HttpContextBase httpContext, Page page, string mobileSuffix) {
        return false;
        //return base.TrySetMobileMasterPage(httpContext, page, mobileSuffix);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的RouteConfig课堂上使用它:

    public static void RegisterRoutes(RouteCollection routes) {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings, new BugFixFriendlyUrlResolver());
    }
Run Code Online (Sandbox Code Playgroud)


小智 7

令人遗憾的是,没有简单的方法来摆脱移动母版页.如果我删除Site.Mobile.Master.master,我结束时出现错误"文件'/ Site.Mobile.Master'不存在".

我为解决这个问题所做的是将以下代码添加到Site.Mobile.Master.cs Page_Load事件中.

var AlternateView = "Desktop";
var switchViewRouteName = "AspNet.FriendlyUrls.SwitchView";
var url = GetRouteUrl(switchViewRouteName, new { view = AlternateView, __FriendlyUrls_SwitchViews = true });
url += "?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);
Response.Redirect(url);
Run Code Online (Sandbox Code Playgroud)