本文整理汇总了C#中RouteCollection.Localization方法的典型用法代码示例。如果您正苦于以下问题:C# RouteCollection.Localization方法的具体用法?C# RouteCollection.Localization怎么用?C# RouteCollection.Localization使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RouteCollection
的用法示例。
在下文中一共展示了RouteCollection.Localization方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegisterRoutes
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.AppendTrailingSlash = true;
routes.LowercaseUrls = true;
routes.MapMvcAttributeRoutes(Localization.LocalizationDirectRouteProvider);
routes.Localization(configuration => {
configuration.DefaultCulture = LocalizationConfig.DefaultCulture;
configuration.AcceptedCultures = LocalizationConfig.AcceptedCultures;
configuration.AttributeRouteProcessing = AttributeRouteProcessing.AddAsNeutralAndDefaultCultureRoute;
configuration.AddCultureAsRoutePrefix = true;
}).Translate(localization =>
{
localization.TranslateInitialAttributeRoutes();
localization.ForCulture("da")
.ForController<HomeController>()
.ForAction(x => x.Index())
.AddTranslation(string.Empty);
});
CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate = Localization.DetectCultureFromBrowserUserLanguages(LocalizationConfig.AcceptedCultures, LocalizationConfig.DefaultCulture);
// Default route fallback
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
示例2: RegisterRoutes
public static void RegisterRoutes(RouteCollection routes) {
/*
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
*/
routes.MapMvcAttributeRoutes(Localization.LocalizationDirectRouteProvider);
Localization localization = routes.Localization(x => {
x.DefaultCulture = "pt";
x.AcceptedCultures = new HashSet<string>() { "pt", "en" };
x.AttributeRouteProcessing = RouteLocalization.Mvc.Setup.AttributeRouteProcessing.AddAsDefaultCultureRoute;
x.AddCultureAsRoutePrefix = true;
x.AddTranslationToSimiliarUrls = true;
});
localization.TranslateInitialAttributeRoutes().Translate(x => {
x.ForCulture("en").ForNamedRoute("home.index").AddTranslation("home");
x.ForCulture("en")
.ForController<AccountController>().ForAction("Login").AddTranslation("home");
x.ForCulture("en")
.ForController<HomeController>().ForAction("OwinTest").AddTranslation("owin-test")
.ForController<HomeController>().ForAction("FormTest").AddTranslation("form-test");
});
CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate = context => { return new CultureInfo("pt"); };
RouteTable.Routes.MapRoute("Start", String.Empty, new { controller = "Home", action = "Start" });
// Missing routes
RouteTable.Routes.MapRoute("404", "{*url}", new { controller = "Error", action = "NotFound_" });
// Register areas
AreaRegistration.RegisterAllAreas();
}
示例3: RegisterRoutes
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// New workflow since 5.2
// This provider wraps generated attribute routes with LocalizationCollectionRoute routes
////LocalizationDirectRouteProvider provider
//// = new LocalizationDirectRouteProvider(new DefaultDirectRouteProvider());
// For less code preparation use the static provider stored in Localization class
routes.MapMvcAttributeRoutes(Localization.LocalizationDirectRouteProvider);
const string defaultCulture = "en";
ISet<string> acceptedCultures = new HashSet<string>() { defaultCulture, "en-US", "de", "de-AT" };
// Add translations
// You can translate every specific route that contains default Controller and Action (which MapMvcAttributeRoutes does)
routes.Localization(configuration =>
{
// Important: Set the route collection from LocalizationDirectRouteProvider if you specify your own
//// configuration.LocalizationCollectionRoutes = provider.LocalizationCollectionRoutes;
configuration.DefaultCulture = defaultCulture;
configuration.AcceptedCultures = acceptedCultures;
// Define how attribute routes should be processed:
// * None: There will be no routes except the ones you explicitly define in Translate()
// * AddAsNeutralRoute: Every attribute route will be added as neutral route
// * AddAsDefaultCultureRoute: Every attribute route will be added as localized route for defined default culture
// * AddAsNeutralAndDefaultCultureRoute: Every attribute route will be added as neutral route and
// as localized route for defined default culture
// * AddAsNeutralRouteAndReplaceByFirstTranslation: Every attribute route will be added as neutral route first, but when
// you add a translation for a route, the neutral route will be removed
configuration.AttributeRouteProcessing = AttributeRouteProcessing.AddAsNeutralAndDefaultCultureRoute;
// Uncomment if you do not want the culture (en, de, ...) added to each translated route as route prefix
configuration.AddCultureAsRoutePrefix = true;
configuration.AddTranslationToSimiliarUrls = true;
}).TranslateInitialAttributeRoutes().Translate(localization =>
{
// Use extension methods if you want to separate route configuration
localization.AddDefaultRoutesTranslation();
localization.AddAreaRoutesTranslation();
// DefaultRoutes.cs
////localization.ForCulture("de")
//// .ForController<HomeController>()
//// .ForAction(x => x.Index())
//// .AddTranslation("Willkommen")
//// .ForAction(x => x.Book())
//// .AddTranslation("Buch/{chapter}/{page}");
////localization.ForCulture("de")
//// .ForController<HomeWithRoutePrefixAttributeController>()
//// .SetRoutePrefix("RoutePrefixDE")
//// .ForAction(x => x.Index())
//// .AddTranslation("Willkommen")
//// .ForAction(x => x.Book())
//// .AddTranslation("Buch/{chapter}/{page}");
////localization.ForCulture("de")
//// .SetAreaPrefix("AreaPrefixDE")
//// .ForController<HomeWithRouteAreaAttributeController>()
//// .ForAction(x => x.Index())
//// .AddTranslation("Willkommen")
//// .ForAction(x => x.Book())
//// .AddTranslation("Buch/{chapter}/{page}");
// AreaRoutes.cs
////localization.ForCulture("de")
//// .SetAreaPrefix("Area")
//// .ForController<Areas.Area.Controllers.HomeController>()
//// .ForAction(x => x.Index())
//// .AddTranslation("Willkommen")
//// .ForAction(x => x.Book())
//// .AddTranslation("Buch/{chapter}/{page}");
});
// Optional
// Setup CultureSensitiveHttpModule
// This Module sets the Thread Culture and UICulture from http context
// Use predefined DetectCultureFromBrowserUserLanguages delegate or implement your own
CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate =
Localization.DetectCultureFromBrowserUserLanguages(acceptedCultures, defaultCulture);
// Optional
// Add culture sensitive action filter attribute
// This sets the Culture and UICulture when a localized route is executed
GlobalFilters.Filters.Add(new CultureSensitiveActionFilterAttribute()
{
// Set this options only if you want to support detection of region dependent cultures
// Supports this use case: https://github.com/Dresel/RouteLocalization/issues/38#issuecomment-70999613
AcceptedCultures = acceptedCultures,
TryToPreserverBrowserRegionCulture = true
});
}