1
votes

I am not sure if it is possible to create complete friendly url (Slug i think they call it in wordpress).

I want to create friendly url dynamically with single route defined in global.asax file.

example :

about-us/

about-us/who-we-are/

about-us/sub-page/sub-page2/

media/news/

media/photo-gallery/

media/press/

at present i have to define route for each url mentioned about.

I have tried in past to create above route with single route defined in gloabal.asax

example

in global.asax file i make call to database table and get information like Page_Name, Page_Handler, Page_Title ...

//Create Route
//some logic to get url segments so that it can be passed to 
getPageDetails(PageURL)
{
//logic to get PageHandler so that route can be created
}

        //For general.aspx Page
        routes.MapPageRoute("Page", "about-us/", "~/Aboutus.aspx", false,
                        new RouteValueDictionary {
                        { "path", "Page-not-found" },{ "PageName", "Page-not-found" }
                    });

How can i create this route dynamically so that i can pass about-us/ & pageHandler dynamically so that one route function call route for all page. If this is possible then i can add as many page to website with making change to global.asax route section by addition additional routes.

Please advice how to call database function to get pagehandler and assign it to route to handle the routing i am not sure if this is possible.

In wordpress they create pages & friendly page name can we achieve same in asp.net web forms.

sample table with data

Page_ID Page_Name       Page_Handler    Page_URL
1       About Us        General.aspx    /about-us/
2       Who we are      General.aspx    /about-us/who-we-are/
3       ...             ..              /about-us/sub-page/sub-page2/
4.      Media           #
5       News            news.aspx       /media/news/
6       Photo Gallery   galler.aspx     /media/photo-gallery/   
2
Do you really need single route, or just want to generate routes dynamically at app start? - Sergio

2 Answers

3
votes

I tried solving your problem using Application_beginRequest event. Below is the code, its not production ready code [Just did a same app], but its an example how can you solve your problem. You might need to add bit more checks and validations according to different scenarios. But shouldn't be much of problem. Please have a read and let me know:

Created a class

public class MaintainUrls
    {
        public int PageId { get; set; }
        public string Page_Name { get; set; }
        public string  Page_Handler { get; set; }
        public string Page_Url { get; set; }

    }

Seed values in class

List<MaintainUrls> urls = new List<MaintainUrls>()
        {
            new MaintainUrls() { PageId = 1, Page_Name = "About Us", Page_Handler = "About.aspx", Page_Url = "/about-us"},
            new MaintainUrls() { PageId = 2, Page_Name = "About Who we were", Page_Handler = "About.aspx", Page_Url = "/about-us/who-we-are/"},
            new MaintainUrls() { PageId = 2, Page_Name = "About Who we were sub page", Page_Handler = "About.aspx", Page_Url = "/about-us/sub-page/sub-page2/"},
            new MaintainUrls() { PageId = 1, Page_Name = "About Us", Page_Handler = "Contact.aspx", Page_Url = "/contact"},
            new MaintainUrls() { PageId = 1, Page_Name = "About Us", Page_Handler = "Default.aspx", Page_Url = "/"},
        }; 

Application_BeginRequest in Global.asax

 void Application_BeginRequest(Object sender, EventArgs e)
        {
            string path = Request.Path.ToLower();

            if (urls.Any(url=> url.Page_Url.Equals(path)))
            {
                string handler = urls.First(url => url.Page_Url.Equals(path)).Page_Handler;
                Context.RewritePath(@"~/" + handler);
            }
        }
0
votes

There is a Nuget package for this that you can easily add to your project:

And here is a quick tutorial:

As mentioned by Scott Hanselman as well, you'd start off with adding this line of code at application start:

RouteConfig.RegisterRoutes(RouteTable.Routes);

If you have a page called AboutUs.aspx, you automatically get an /AboutUs route. Just add the package and call

routes.EnableFriendlyUrls();

in RouteConfig (this default comes with the NuGet package) and the whole WebForms project loses its .aspx extensions and gets the routes you want.