Search code examples
asp.netweb-configurl-routingasp.net-4.0url-mapping

How to specify url mappings in ASP.NET 4.0 when parameters are involved?


The problem

I have a web site with some articles inside. Articles are accessed using a database writing results on a single page. When a certain article is to be accessed, I need the article-id and I only need to get to the page: http://www.mysite/Articles.aspx?a={article-id}.

This is nice but not really friendly and not scalable. What I want to do is to redirect every url in the form: http://www.mysite/articles/{article-id} to the page http://www.mysite/Articles.aspx?a={article-id}.

I know that using Web.Config I can use the urlMappings settings, but do not know how to insert a regex there.

Some notes

Please consider that I would like to have a simple translation tool. I do not want to edit my existing pages. This solution should be as transparent as possible to my application components.

What is the best practice to get to the solution?


Solution

  • I think you should use:

    1. ASP.NET Routing

    Eg.:

    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }
    
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("Articles",
            "articles/{article-id}/",
            "~/Articles.aspx");
    }
    

    & on "Articles.aspx.cs" you can get the "article-id" using:

    string article-id = Page.RouteData.Values["article-id"] as string;
    
    1. Using the URL Rewrite Module

    enter image description here


    1. urlrewriter.net

    enter image description here