EKsumic's Blog

let today = new Beginning();

Click the left button to use the catalog.

OR

ASP.NET WebForms 中的 RouteConfig.cs 簡介

在 ASP.NET WebForms 項目中,RouteConfig.cs 主要用於配置 URL 路由。通過啟用「友好 URL」(Friendly URLs)功能,開發者可以讓 URL 更加簡潔,提升用戶體驗和 SEO 表現。

1. 什麼是 Friendly URLs?

Friendly URLs 是 ASP.NET 提供的一個功能,允許將像 /Default.aspx 這樣的 URL 自動轉換成 /Default,去掉 .aspx 擴展名。這讓 URL 看起來更簡潔美觀,對搜索引擎也更友好。

2. RouteConfig.cs 文件結構

你的 RouteConfig.cs 主要通過 FriendlyUrlSettingsEnableFriendlyUrls 來實現自動 URL 簡化:

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
}
  • FriendlyUrlSettings:配置 URL 行為,AutoRedirectMode 設為 Permanent 表示當用戶訪問包含 .aspx 的 URL 時,會永久重定向到去掉擴展名的版本。
  • EnableFriendlyUrls:啟用友好 URL 功能,無需手動配置每個路由。

3. 作用與優勢

  • 美化 URL:自動去掉 .aspx,讓 URL 更加簡潔。
  • SEO 友好:支持 301 永久重定向,增強搜索引擎優化。
  • 簡化配置:無需手動定義每個路由規則。

4. 總結

RouteConfig.cs 使用 Friendly URLs 是一個簡單但有效的功能,它提升了 WebForms 項目的可讀性和易用性。如果你希望項目的 URL 更加美觀而不影響功能,這是非常值得使用的配置。

This article was last edited at 2024-10-03 09:51:46

Today's comments have reached the limit. If you want to comment, please wait until tomorrow (UTC-Time).

There is 20h34m08s left until you can comment.