默认文件夹路由使用Microsoft.AspNet.FriendlyUrls

本文关键字:Microsoft AspNet FriendlyUrls 文件夹 路由 默认 | 更新日期: 2023-09-27 18:08:52

我正在使用Microsoft.AspNet.FriendlyUrls开始一个新的webforms项目,但希望能够为文件夹设置默认路由。我有一个名为news的文件夹,其中包含新闻。Aspx和newsitem.aspx。我希望能够路由如下:

http://sitename/news - Routes to ~/news/news.aspx
http://sitename/news/news - Routes to ~/news/news.aspx
http://sitename/news/newsitem - Routes to ~/news/newsitem.aspx

第二条和第三条路由使用下面的代码工作,但不使用http://sitename/news

 public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
        routes.MapPageRoute("NewsDefault", "news", "~/news/news.aspx");
    }
}

默认文件夹路由使用Microsoft.AspNet.FriendlyUrls

发现解决方案

需要添加routes.RouteExistingFiles = true;

public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);
            routes.RouteExistingFiles = true;
            routes.MapPageRoute("NewsDefault", "news", "~/news/news.aspx");
        }