I am using ASP.NET MVC localized routes. So when a user goes to the English site it is example.com/en/Controller/Action and the Swedish site is example.co
Ok .. another suggestion.
To make sure I understand, you want ..
if so .. this answer has three parts:-
global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}",
new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"Question-Answer", // Route name
"{languageCode}/{controller}/{action}", // URL with parameters
new {controller = "home", action = "index"} // Parameter defaults
);
}
So, if you want to have the route http://www.example.com/sv/account/logon then the above route will work.
LanguageCode == sv (or en or fr or whatever language you're supporting)
account == the controller: AccountController
login == action.
the fact that i've said controller = "home" and action="index" only mean that those two parameters are defaulted to those values IF none were provided. So, if you goto http://www.example.com/sv/account/logon then the MVC framework is smart enough to know (based on that route) that the languageCode paramters == sv, controller == action and action (method) == index.
NOTE: The order of your routes is IMPORTANT. critically important. This route needs to be one (if not the) of the first routes (after the IgonoreRoute's) when you register your routes.
.
using System.Linq;
using System.Web.Mvc;
namespace YourNamespace.Web.Application.Models
{
public class LanguageCodeActionFilter : ActionFilterAttribute
{
// This checks the current langauge code. if there's one missing, it defaults it.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
const string routeDataKey = "languageCode";
const string defaultLanguageCode = "sv";
var validLanguageCodes = new[] {"en", "sv"};
// Determine the language.
if (filterContext.RouteData.Values[routeDataKey] == null ||
!validLanguageCodes.Contains(filterContext.RouteData.Values[routeDataKey]))
{
// Add or overwrite the langauge code value.
if (filterContext.RouteData.Values.ContainsKey(routeDataKey))
{
filterContext.RouteData.Values[routeDataKey] = defaultLanguageCode;
}
else
{
filterContext.RouteData.Values.Add(routeDataKey, defaultLanguageCode);
}
}
base.OnActionExecuting(filterContext);
}
}
}
here we go ... (pseudo code again....)
public abstract class BaseController : Controller
{
protected string LanguageCode
{
get { return (string) ControllerContext.RouteData.Values["LanguageCode"]; }
}
}
So then we decorate our controllers like this :)
[LanguageCodeActionFilter]
public class ApiController : BaseController
{
public ActionResult Index()
{
if (this.LanguageCode == "sv") ... // whatever.. etc..
}
}
Notice how i've decorated the class .. not just each action. this means ALL actions in the class will be affected by the ActionFilter :)
Also, you might want to add a new route in the global.asax that handles NO languageCode .. and hardcode default that value ...
like (also untested) ...
routes.MapRoute(
"Question-Answer", // Route name
"{controller}/{action}", // URL with parameters
new {controller = "home", action = "index", languageCode = "sv"} // Parameter defaults
);
Does this help?