在现有 ASP.NET WebForms 站点中添加带有根路径的第二语言支持

2024-02-24

我继承了一个非常小的 ASP.NET WebForms 项目,我的客户希望为其添加第二种语言。

对于每个“somepage.aspx”,我想支持它的“第二语言路径”版本,例如“fr/somepage.aspx”。我想使用正常的全球化(两种语言的 CurrentCulture + 资源文件)来处理这个问题,并避免重复每个页面。我必须保持原始路径有效,因此我暂时排除了 ASP.NET MVC(因为不知道我是否可以继续支持“.aspx”路径)。

这可能吗?


URL 路由可用于 ASP.NET。

您可以创建两条路线,第一个是捕捉您的语言的路线:

{语言}/{页面}

第二条路线就是

{page}

在 MVC 中,我们可以创建路由约束来强制语言具有特定值(例如 en、en-us 等),我不确定是否可以在常规 ASP.NET WebForms 路由中完成同样的操作。

这里有两篇文章描述了 WebForms 中的路由主题(非 MVC)

http://msdn.microsoft.com/en-us/magazine/dd347546.aspx http://msdn.microsoft.com/en-us/magazine/dd347546.aspx

and

http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series。 ASPX http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx

编辑以添加代码示例

在我的 Global.asax 中,我注册了以下内容:

    void RegisterRoutes(RouteCollection routes)
    {
        routes.Ignore("{resource}.asxd/{*pathInfo}");
        routes.Add(
            new Route(
                "{locale}/{*url}", //Route Path
                null, //Default Route Values
                new RouteValueDictionary{{"locale", "[a-z]{2}"}}, //constraint to say the locale must be 2 letters. You could also use something like "en-us|en-gn|ru" to specify a full list of languages
                 new Utility.Handlers.DefaultRouteHandeler() //Instance of a class to handle the routing
            ));

    }


    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoutes(RouteTable.Routes);

    }

我还创建了一个单独的类(参见asp.net 4.0 Web 表单路由 - 默认/通配符路由 https://stackoverflow.com/questions/2704338/asp-net-4-0-web-forms-routing-default-wildcard-route作为指导。)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.UI;

namespace SampleWeb.Utility.Handlers
{
    public class DefaultRouteHandeler:IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            //Url mapping however you want here: 

            string routeURL = requestContext.RouteData.Values["url"] as string ;

            string pageUrl = "~/" + (!String.IsNullOrEmpty(routeURL)? routeURL:""); 

            var page = BuildManager.CreateInstanceFromVirtualPath(pageUrl, typeof(Page))
                       as IHttpHandler;
            if (page != null)
            {
                //Set the <form>'s postback url to the route 
                var webForm = page as Page;
                if (webForm != null)
                    webForm.Load += delegate
                    {
                        webForm.Form.Action =
                        requestContext.HttpContext.Request.RawUrl;
                    };
            }
            return page;
        }
    }
}

这是可行的,因为当 URL 中未指定区域设置时,Web 表单的默认视图引擎将接管。当使用 2 个字母的语言环境(en?us?等)时,它也适用。在 MVC 中,我们可以使用 IRouteConstraint 并执行各种检查,例如确保区域设置在列表中、检查路径是否存在等,但在 WebForms 中,约束的唯一选项是使用 RouteValueDictonary。

现在,我知道代码按原样存在问题,默认文档无法加载。所以http://localhost:25436/en/ http://localhost:25436/en/不加载default.aspx的默认文档,但是http://localhost:25436/en/default.aspx http://localhost:25436/en/default.aspx确实有效。我会把这个问题留给你来解决。

我用子目录测试了它并且它有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在现有 ASP.NET WebForms 站点中添加带有根路径的第二语言支持 的相关文章

随机推荐