IIS 和 ASP.NET 堆栈上短 URL 的设计和实现

发布于 2024-10-20 17:44:51 字数 555 浏览 5 评论 0原文

我有兴趣在网站上创建“短 URL”页面片段。然而,这并不像 bit.ly 那样传统意义上的“短 URL”,它会重定向到不同的目标 URL。我希望短网址成为最终目的地。

例如,这些 URL 之一可能是 http://foo/a/Afjbg,当您导航到它时,它停留在 http://foo/a/Afjbg (IOW,http://foo/a/Afjbg 对用户在地址栏中可见)。

如果是静态内容,我只需将页面和文件夹排列到这些名称中即可。但网站上的内容将从数据库动态驱动,因此每个页面都是动态生成的。因此,内容在逻辑上看起来不同,但实际上本质上是相同的具有动态内容的 .aspx 页面。

如何在 Microsoft 托管堆栈上实现这一点?该平台是带有 ASP.NET 4 的 IIS 7。我认为有一种方法可以轻松设置它,但作为 MS 托管堆栈的新手,我不知道:)

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).

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紅太極 2024-10-27 17:44:51

使用 ASP.NET MVC 路由

它允许路由任何 URL 模式到“页面”,

例如

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

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
            );
白鸥掠海 2024-10-27 17:44:51

它称为 URL 路由,ASP.NET 从 3.5 版开始就原生支持它。下面是一个 C# 示例,取自 MSDN。波形括号将作为参数发送到 ~/categoriespage.aspx 的 URL 路径块分开。

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

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

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");
}
朮生 2024-10-27 17:44:51

您可以使用大多数基于 .NET 的 CMS博客引擎 并制作 slug 一个短字符串。

详细信息:

在大多数 CMS 和博客引擎中,软件将 slug 与帖子标题分开。如果您不指定 slug,它会自动为您生成一个 slug...例如,一篇标题为“Hello world”的帖子可能会生成一个生成的 slug“hello_world”。但您可以输入自己的别名“Afjbg”。

或者,如果您想变得更复杂一些,我上面引用的两个程序都是开源的 - 这意味着您可以轻松修改 slug 生成组件以自动生成这些小字符串。

您可以尝试使用 ASP.NET 路由,但如果不了解有关您正在使用或构建的应用程序的更多信息,这可能不太容易实现(即某些 CMS 引擎可能已经使用路由,或者使用不支持路由的老式处理程序)玩得很好)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文