C# asp.net mvc路由中的翻转路由值
我是asp.NETMVC新手,请耐心等待 我们有以下路由字典设置C# asp.net mvc路由中的翻转路由值,c#,asp.net,asp.net-mvc,url-routing,C#,Asp.net,Asp.net Mvc,Url Routing,我是asp.NETMVC新手,请耐心等待 我们有以下路由字典设置 routes.MapRoute( "Default", // Route name "{language}/{controller}/{action}/{id}", // URL with parameters new
routes.MapRoute(
"Default", // Route name
"{language}/{controller}/{action}/{id}", // URL with parameters
new { language = "en", controller = "Home", action = "Index", id = "" } // Parameter defaults
);
对于我们应用程序中的任何给定页面,我们都需要提供指向同一页面的法语版本的链接。例如,页面:
http://www.example.com/en/home
应该在该页面上有指向
http://www.example.com/fr/home
现在我有了以下UrlHelper扩展方法
public static string FilpLanguage(this UrlHelper urlHelper)
{
var data = urlHelper.RequestContext.RouteData;
if (System.Threading.Thread.CurrentThread.CurrentCulture == CultureInfo.GetCultureInfoByIetfLanguageTag("en-CA"))
data.Values["language"] = "fr";
else
data.Values["language"] = "en";
return urlHelper.RouteUrl(data.Values.Where(item => item.Value != null));
}
但是,在www.example.com/en/home上调用FilpLanguage将返回以下URL:
www.example.com/en/home?current=[,]
我是不是遗漏了什么?当前参数来自哪里
提前谢谢
更新
我发现我可以在当前请求上下文中简单地使用RouteValueDictionary
public static MvcHtmlString GetOtherLanguageLink(this HtmlHelper html, string linkText)
{
var routeDictionary = html.ViewContext.RouteData.Values;
if (null == routeDictionary["action"] || !(routeDictionary["action"] is string) ||
null == routeDictionary["controller"] || !(routeDictionary["controller"] is string))
throw new ArgumentException("Either action or controller is missing in routeData.Values (RouteValueDictionary)");
if (System.Threading.Thread.CurrentThread.CurrentCulture == CultureInfo.GetCultureInfoByIetfLanguageTag("en-CA"))
routeDictionary["language"] = "fr";
else
routeDictionary["language"] = "en";
// add query strings as extra route values
foreach (string key in html.ViewContext.RequestContext.HttpContext.Request.QueryString.AllKeys)
if (!string.IsNullOrEmpty(key) && !routeDictionary.ContainsKey(key))
routeDictionary.Add(key, html.ViewContext.RequestContext.HttpContext.Request.QueryString[key]);
return html.ActionLink(linkText, routeDictionary["action"].ToString(), routeDictionary["controller"].ToString(), routeDictionary, null);
}
请将您的更新添加为答案并接受,然后问题将得到回答!!。