Default folder routes using Microsoft.AspNet.Frien

2019-08-08 19:36发布

问题:

I'm starting a new webforms project using Microsoft.AspNet.FriendlyUrls but want to be able to set a default route for a folder. I have a folder called news which contains news.aspx and newsitem.aspx. I'd like to be able to route as follows:

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

The second and third routes work using the code below but not 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");

    }
}

回答1:

Found Solution

Needed to add 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");
        }