开发者

Design and implementation for Short URLs on IIS and ASP.NET stack

I'm interested in creating "short URLs" a segment of pages on a site. However, this isn't in the traditional sense of "short URLs" like bit.ly where it will redirect to a different destination URL. I want the short URL to be the final destination.

For example, one of these URLs might be http://foo/a/Afjbg, and when you navigate to it, it stays on http://foo/a/Afjbg (IOW, http://foo/a/Afjbg is visible to the user in the address bar开发者_C百科).

If it was static content, I would just arrange the pages and folders into these names. But the content I will have on the site will be dynamically driven from a DB, so each page is generated on the fly. So the content looks logically different, but in reality is essentially the same .aspx page with dynamic content.

How can this be accomplished on a Microsoft hosting stack? The platform is IIS 7 with ASP.NET 4. I figure there is a way to easily set this up, but being new to the MS hosting stack, I have no idea :)


Use ASP.NET MVC routing

It allows routing of any URL pattern to a "page"

e.g.

routes.MapRoute(
                "Default", // Route name
                "a/{id*}",   // Route anything to this controller
                new { controller = "Home", action = "Index", id = "" } // Parameter defaults
            );


It's called URL routing, and ASP.NET supports it natively since version 3.5. Here is an example in C#, taken from MSDN. The squiggly brackets individuate chunks of the URL path that get sent as parameters to ~/categoriespage.aspx.

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
        "Category/{action}/{categoryName}",
        "~/categoriespage.aspx");
}


You could use most any .NET-based CMS or blog engine and just make the slug a short string.

Details:

In most CMS and blogging engines, the software separates the slug from post title. It will auto-generate one for you if you don't specify a slug... for example, a post titled "Hello world" might get a generated slug of "hello_world". But you can type in your own slug to be "Afjbg".

Or if you want to get a little more sophisticated, both of the programs I cited above are open-source - which means you can easily modify the slug generation component to auto-generate these little strings.

You could try to use ASP.NET routing, but without knowing more about the application you're using or building, that might not work out easily (i.e. some CMS engines might already use routing, or use old-school handlers that don't play nicely with it).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜