在现有的ASP.NET WebForms站点中使用根路径添加第二语言支持 [英] Add second language support with root path in an existing ASP.NET WebForms site

查看:60
本文介绍了在现有的ASP.NET WebForms站点中使用根路径添加第二语言支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承自一个很小的ASP.NET WebForms项目,我的客户想为其添加第二种语言.

I have inherited from a very small ASP.NET WebForms project, and my customer would like to add a second language to it.

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

For every "somepage.aspx", I'd like to support a "second language path" version of it, like "fr/somepage.aspx". I'd like to handle this using normal globalization (CurrentCulture + resource files in both languages) and avoid having to duplicate each page. I must keep the original paths valid, thus I have excluded ASP.NET MVC for now (for lack of knowing if I could continue to support ".aspx" paths).

这可能吗?

推荐答案

URL路由在ASP.NET中可用.

URL Routing is avalaible in for ASP.NET.

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

You could create two routes, the first being the route that catches your language:

{语言}/{页面}

{language}/{page}

第二条路线就是

{page}

在MVC中,我们可以创建将语言强制为特定值的路由约束(例如en,en-us等).如果可以在常规ASP.NET WebForms路由中完成相同的操作,我不是很肯定

In MVC we can create route constraints that would enforce the Language to be of a specific value (so like en, en-us, etc) I'm not positive if the same can be done in regular ASP.NET WebForms routing.

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

Here are two articles that describe the topic of routing in WebForms (non-MVC)

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

已编辑以添加代码示例

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

In my Global.asax I registered the following:

    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表单路由-默认/通配符路由作为指南.)

I also created a seperate Class (see asp.net 4.0 web forms routing - default/wildcard route as a guide.)

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.

This works because when no locale is specified in the URL the default view engine for Web Forms takes over. It also works when a 2 letter locale (en? us? etc) is used. In MVC we can use an IRouteConstraint and do all kinds of checking, like making sure the locale is in a list, checking to see if the path exists, etc but in WebForms the only option for a constraint is using a RouteValueDictonary.

现在,我知道代码原样存在问题,默认文档不会加载.因此 http://localhost:25436/en/不会加载default.aspx的默认文档,但是 http://localhost:25436/en/default.aspx 可以工作.我会留给你解决.

Now, I know there is an issue with the code as-is, default documents don't load. So http://localhost:25436/en/ does not load the default document of default.aspx, but http://localhost:25436/en/default.aspx does work. I'll leave that to you to resolve.

我使用子目录对此进行了测试,并且可以正常工作.

I tested this with sub directories and it works.

这篇关于在现有的ASP.NET WebForms站点中使用根路径添加第二语言支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆