在 ASP.NET 友好 URL 中如何实现?
谢谢。
删除Site.Mobile.Master页面,友好的URL将使用常规的Site.Master页面。
site.mobile.master
时在友好的URL代码中出现了 "The relative virtual path 'Site.Mobile.Master' is not allowed here."
的错误。我就是因为这个问题而受挫了。/// <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);
}
}
然后我在我的RouteConfig
类中使用它:
public static void RegisterRoutes(RouteCollection routes) {
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings, new BugFixFriendlyUrlResolver());
}
RouteConfig
中注册在“App_Start”中,如所示。 RouteConfig
是从Global.asax.cs
调用的。 - MikeBaz - MSFT很奇怪的是,没有简单的方法可以摆脱移动主页面。如果我删除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);
protected void Page_Load(object sender, EventArgs e)
{
CurrentView = "Desktop";
AlternateView = "Desktop";
// 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 switch view route is out of sync
this.Visible = false;
return;
}
var url = GetRouteUrl(switchViewRouteName, new { view = AlternateView, __FriendlyUrls_SwitchViews = true });
url += "?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);
SwitchUrl = url;
}
3) 直到我删除整个目录并重新发布它时,这才起作用。我认为,删除一些特定的文件可能也有帮助。但是由于我没有那样的环境,所以我选择了更简单的方式。
我发现直接删除(或重命名)Site.Mobile.Master和ViewSwitcher.ascx更容易。这对我来说似乎完全有效。
Protected Sub Page_Load(sender As Object, e As EventArgs) ' Determinar la vista actual Dim isMobile = WebFormsFriendlyUrlResolver.IsMobileView(New HttpContextWrapper(Context)) CurrentView = If(isMobile, "Mobile", "Desktop")
' Determinar la vista alternativa AlternateView = If(isMobile, "Desktop", "Mobile") ' Create URL de conmutador a partir de la ruta, p. ej. ~/__FriendlyUrls_SwitchView/Mobile?ReturnUrl=/Page Dim switchViewRouteName = "AspNet.FriendlyUrls.SwitchView" Dim switchViewRoute = RouteTable.Routes(switchViewRouteName) If switchViewRoute Is Nothing Then ' Las URL descriptivas no están habilitadas o el nombre de la ruta de la vista del conmutador no está sincronizado Me.Visible = False Return End If Dim url = GetRouteUrl(switchViewRouteName, New With { .view = AlternateView, .__FriendlyUrls_SwitchViews = True }) url += "?ReturnUrl=" & HttpUtility.UrlEncode(Request.RawUrl) SwitchUrl = url **Response.Redirect(url)** End Sub