Is it possible to localize a URL / routing in ASP.NET MVC? - asp.net-mvc

I'm working with a client that wants the URLs in our web application to be in French. I'm an English developer and we also have English clients. This is an interesting problem but I don't think its something the ASP.NET MVC Framework would support.
Here's the scenario. The route...
Specific EXAMPLE
English URL
www.stackoverflow.com/questions/ask
would also support
French URL
www.stackoverflow.com/problème/poser
Generic EXAMPLE
English URL
http://clientA.product.com/AreaNameEnglish/ControllerNameEnglish/ActionNameEnglish/params
also needs to support
French URL
http://clientB.product.com/AreaNameFrench/ControllerNameFrench/ActionNameFrench/params
So in MVC my Area, Controller and Actions all need to have both English and French translations.
Obviously maintainability would be a HUGE issue if I were to go and hardcode all my Controllers, Views and Action names to French. Is there anyway to localize the route that is presented in the browser without doing this? Keeping in mind there are lots of different routes in the application. A couple Areas each with a handful of Controller each with many Actions?
Thanks,
Justin
EDIT
Thanks to #womp here is what I've come up with so far... Although in the end I took the approach which I posted as an answer.
public class LocalizedControllerFactory : DefaultControllerFactory
{
public override IController CreateController(RequestContext requestContext, string controllerName)
{
if (string.IsNullOrEmpty(controllerName))
throw new ArgumentNullException("controllerName");
if (CultureInfo.CurrentCulture.TwoLetterISOLanguageName == "fr")
{
controllerName = this.ReplaceControllerName(requestContext, controllerName);
this.ReplaceActionName(requestContext);
this.ReplaceAreaName(requestContext);
}
return base.CreateController(requestContext, controllerName);
}
private string ReplaceControllerName(RequestContext requestContext, string controllerName)
{
// would use the language above to pick the propery controllerMapper. For now just have french
Dictionary<string, string> controllerMapper = new Dictionary<string, string>()
{
{"frenchControllerA", "englishControllerA"},
{"frenchControllerB", "englishControllerB"}
};
return this.ReplaceRouteValue(requestContext, "controller", controllerMapper);
}
private void ReplaceAreaName(RequestContext requestContext)
{
// would use the language above to pick the propery areaMapper. For now just have french
Dictionary<string, string> areaMapper = new Dictionary<string, string>()
{
{"frenchAreaX", "englishAreaX"},
{"frenchAreaY", "englishAreaY"}
};
this.ReplaceRouteValue(requestContext, "area", areaMapper);
}
private void ReplaceActionName(RequestContext requestContext)
{
// would use the language above to pick the propery actionMapper. For now just have french
Dictionary<string, string> actionMapper = new Dictionary<string, string>()
{
{"frenchAction1", "englishAction1"},
{"frenchAction2", "englishAction2"}
};
this.ReplaceRouteValue(requestContext, "action", actionMapper);
}
private string ReplaceRouteValue(RequestContext requestContext, string paramName, Dictionary<string, string> translationLookup)
{
if (requestContext.RouteData.Values[paramName] == null)
{
return null;
}
string srcRouteValue = requestContext.RouteData.Values[paramName] as string;
if (srcRouteValue != null && translationLookup.ContainsKey(srcRouteValue))
{
requestContext.RouteData.Values[paramName] = translationLookup[srcRouteValue];
}
return requestContext.RouteData.Values[paramName] as string;
}
}
A decent start. If I localize just the ControllerName and ActionName in the Url it will find and render the proper View. However I have the following problems.
Area Name can't be translated
Localizing the Area means the Controller.View() method fails to find Views.
Even though I've replaced the Area name in the request context the ViewEngineCollection.Find() method doesn't seem to pick it up. Anywhere in my Controller class that does "return View()" fails to find the default view for its action. If I don't localize the Area then the other steps work.
RedirectToAction or Html.ActionLink
Anytime the application calls RedirectToAction or if I use an Html.ActionLink helper or something similiar the Urls generate are the English ones. It looks like I'm going to have to add logic somewhere possibly in multiple spots to convert an English Url to the French (or other language) one.

The following blog contains a complete solution this exact problem. Its actually a very elegant solution which I highly recommend.
https://blog.maartenballiauw.be/post/2010/01/26/translating-routes-(aspnet-mvc-and-webforms).html
Note to get it working for AREAs I had to add the following extension method to his "TranslatedRouteCollectionExtensions.cs" class:
public static Route MapTranslatedRoute(this AreaRegistrationContext areaContext, string name, string url, object defaults, object routeValueTranslationProviders, bool setDetectedCulture)
{
TranslatedRoute route = new TranslatedRoute(
url,
new RouteValueDictionary(defaults),
new RouteValueDictionary(routeValueTranslationProviders),
setDetectedCulture,
new MvcRouteHandler());
route.DataTokens["area"] = areaContext.AreaName;
// disabling the namespace lookup fallback mechanism keeps this areas from accidentally picking up
// controllers belonging to other areas
bool useNamespaceFallback = (areaContext.Namespaces == null || areaContext.Namespaces.Count == 0);
route.DataTokens["UseNamespaceFallback"] = useNamespaceFallback;
areaContext.Routes.Add(route);
return route;
}
However, even with this a translated route with an AREA can be read and interpreted the routes generated always seem to include an English AREA name but localized everything else.
I was directed to a blog via the same question asked on the ASP.NET MVC Forums

The MVC framework supports pretty much any routing scenario you can think of, but not necessarily with the default routing classes.
Most localization solutions I've run across involve using the same Controller and Action method names, but specifying a culture parameter in the route which dictates which translated version of the View is presented. For example,
http://clientA.product.com/AreaName/Controller/Action //en-US
http://clientB.product.com/es-MX/AreaName/Controller/Action // spanish
If you really must have translated URL's though, I don't see much other choice then to maintain a mapping table somewhere. If I understand your question correctly, you need to be able to map all the different language translations of "questions" (controller) and "ask" (action) to the same controller/action method combination.
However, once you've built this table somewhere (resource files?), you can easily override the DefaultControllerFactory that the framework is using, and implement your own logic for determining the controller to instantiate. So instead of just matching the {controller} token from the URL as a simple string comparison, you can implement logic to check it against your mapping table to pick the right controller.
For a walkthrough of creating a custom controller factory, check this great blog post. It's actually a localization example as well, but it's based on the user's culture settings, rather than the language of the URL.

Related

Unable to define external URL for MvcSiteMapProvider with DynamicNode

I've been pondering this problem for hours now, and it looks to me that while the XML site map functionality was updated to accept external URLs with the URL attribute, that feature was not extended to dynamic definitions via DynamicNode.
We have several applications that share a menu, and thus we need a single definition for these menu items, each pointing to one of our applications in the intranet. We currently use (for ASP.NET) a custom site map that requires the XML files be deployed to each application, and I want the MVC implementation to read the items dynamically - probably from a database.
Since DynamicNode does not have a URL property, it clearly requires that the path be virtual and based upon the other properties (controller, action, routes). I tried implementing a UrlResolver to look at "action" for "HTTP", which worked, and I returned the URL unchanged. However, I receive the dreaded
"http://site.com/App is not a valid virtual path."
error. It appears the change introduced by the author in 3.1.0 for the XML property did not translate to the dynamic equivalent. Can anyone help me? Here is the code I wrote to extend the default classes. This code is executed and works fine. Sometime after ResolveUrl returns my HTTP, the failure occurs, so I guess I'm either missing something or it cannot be done.
public class DynamicSiteMapNodeProvider : DynamicNodeProviderBase
{
override public IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
var nodes = new List<DynamicNode>();
nodes.Add(new DynamicNode
{
Key = "Dynamic1",
Title = "Dynamic One!",
Action = "http://my.site.com/MyApp"
});
return nodes;
}
}
public class FlexibleSiteMapUrlResolver: DefaultSiteMapNodeUrlResolver
{
public override string ResolveUrl(MvcSiteMapNode mvcSiteMapNode, string area, string controller, string action, IDictionary<string, object> routeValues)
{
// when action value is clearly a web site url, simply accept it.
if (action.Substring(0, 5).ToLower() == "http:")
{
return action;
}
return base.ResolveUrl(mvcSiteMapNode, area, controller, action, routeValues);
}
}
5/15/2013: Is there nobody who can help with this?
This was fixed in version 4 - the URL property has been added to the DynamicNode object and works the same as it does everywhere else.
Version 4 was released a couple of weeks ago and is available on the NuGet.

Replace character in url before routing in ASP.NET MVC

Can I manipulate the url before routing it, i.e. before MVC goes through my route configuration to find the route to use.
I'd like to replace some characters in the url, for example "www.test.com/ä/ö" to "www.test.com/a/o". That way, if a user typed those letter in the url, the right route would still be used.
Maybe there´s something that I can hook into to manipulate the url?
Edit:
To clarify what I want I'll add an example. Let's say I have a routing configuration that looks like this: "{controller}/{action}". The user types www.test.com/MyCöntroller/MyÄction and I want to route that to the controller "MyController" and the action method "MyAction". I have to do the character replacement before the routing is done, otherwise no matching route will be found. Thus I'd like to replace all "ö" with "o" and all "ä" with "a" (and some more characters) BEFORE the routing is done. Is there any way to do this?
Edit2:
After some research it seems like it is UrlRoutingModule that is the first to get the url in ASP.NET MVC. Maybe there is some way to hook into that?
Take a loot at this post, by creating custom route handler it is possible.
using System.Web.Routing;
namespace My.Services
{
public class MyRouteHander : IRouteHandler
{
ApplicationDbContext Db = new ApplicationDbContext();
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
// Get route data values
var routeData = requestContext.RouteData;
var action = routeData.GetRequiredString("action");
var controller = routeData.GetRequiredString("controller");
//modify your action name here
requestContext.RouteData.Values["action"] = actionName;
requestContext.RouteData.Values["controller"] = "SpecialController";
return new MvcHandler(requestContext);
}
}
}
Check out the answer to this question.
Basically you'll want to use the FilterAttribute with IActionFilter, and then apply the annotation to the ActionResult that services the route. This way you have an intermediary method to manipulate the URL before it's processed by your route configuration.

ASP.NET MVC 3 Reroute to other controller and action & Restful API versioning

My original problem is I am looking what is the best practise to do versioning in Restful API. Not much people talk about this, dont have a good answer or I can't found exactly the solution at this moment.
(1) At first I am thinking to use Tag or Branch for each version http://domain.com/API/{version}. So if new API released, I Tag it, export and publish into the respective URL but seems hard to mix the different revision of source in one web application.
(2) Then I am thinking to use this way, one controller for one version:
(Just like this question Versioning of REST API Built With ASP.NET MVC 3 - Best Practices)
http://domain.com/API/1.0/{AnAction} => will go to APIV1Controller.{AnAction}
http://domain.com/API/2.0/{AnAction} => will go to APIV2Controller.{AnAction}
but it need to write a route for each version.
(3) Third way I get the idea from PayPal API which is the version is not in the URL but in the POST parameter. So the URL fixed to http://domain.com/API/ but user must specify the Version parameter to have "1.0" or "2.0".
The solution for this: The (2) is ok for me, and currently I use this way but I want to mixed the (2) and (3) so I have a APIController which only have one Index action to check this Version parameter and transfer the request to the respective controller and action either APIV1Controller.{AnAction} or APIV2Controller.{AnAction}.
After Googling and Stackoverflowing about how to transfer, invoke or call another controller and action without redirection. Seems there is no good answer and good practise. Someone answer .NET MVC Call method on different controller by simply creating new instance of the controller. Suddenly I got the idea how about to reroute!
The question:
Is it possible to reroute the the other controller and action from another action without redirection and how to do that?
Or a specific question, when user request http://domain.com/API/{AnAction} with Version="2.0", how can I reroute from APIController.Index to APIV2Controller.{AnAction}?
I am not using IoC.
This can be done via routing constraints. Firstly implement IRouteConstraint:
public class RequestParameterConstraint : IRouteConstraint
{
public string ParameterName { get; private set; }
public string ParameterValue { get; private set; }
public RequestParameterConstraint(string parameter, string value)
{
ParameterName = parameter;
ParameterValue = value;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection)
{
var value = httpContext.Request[ParameterName] ?? "";
return value.Equals(ParameterValue);
}
}
Then register routes:
routes.MapRoute(
"Version10",
"API/{action}/{id}",
new { controller = "APIV1", action = "Index", id = UrlParameter.Optional },
new { header = new RequestParameterConstraint("Version", "1.0") }
);
routes.MapRoute(
"Version20",
"API/{action}/{id}",
new { controller = "APIV2", action = "Index", id = UrlParameter.Optional },
new { header = new RequestParameterConstraint("Version", "2.0") }
);
That's all. This will do the trick.
http://coderjournal.com/2010/09/simple-rest-api-versioning-using-mef-and-mvc/
This seems to do exactly what you want, but it takes a very different approach by using a completely different set of controllers, rather than rerouting to them.
Hope this helps.

ASP.Net MVC 3 Display-templates and Editor-Template Custom Location, how to?

i am going Nuts,
i am using MVCContrib, to create pluggable site using Portable Areas, and everything is working well so far, except that when i started using MVC Templates, what is happening is if i put the The templates in the respective folder of the View it works, examples
HostApplication/Views/Home/DisplayTemplates/FirstName.cshtml
HostApplication/Areas/PortableArea_Blog/Views/Home/DisplayTemplates/Auther.cshtml
but what i want really is the ability to create common templates Set and utilize it from either Host Application or Portable Area, so to do that i created a new Portable Area Called DisplayTemplates(to utilize MVCContrib Ability to compile Views), here is the portable Area structure
DisplayTemplates
|-Views
|-CommentTemplate.cshtml
now in my host Application i have created a Test Model and added UIHint Attribute
public class HostModel
{
[UIHint("~/Areas/DisplayTemplates/Comment.cshtml")]
public string Name { get; set; }
}
but it is not working, so i thought it has something to do with Partial Views Location so i created a CustomView Engine to find Partial Views in that Location and registerd it in Global.asax, here is a short idea about so i wont bore you with full code
public class AreaViewEngine : RazorViewEngine
{
public AreaViewEngine()
{
// {0} = View name
// {1} = Controller name
// View locations
ViewLocationFormats = new[]
{
"~/Areas/DisplayTemplates/{0}.cshtml"
};
PartialViewLocationFormats = ViewLocationFormats;
AreaPartialViewLocationFormats = ViewLocationFormats;
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return new RazorView(controllerContext, partialPath, null, true, new[] { "cshtml" });
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return new RazorView(controllerContext, viewPath, masterPath, true, new[] { "cshtml" });
}
}
what is even more weird, is that it seems that that UIHint with Explicit location to Display Template, does not work, here is an example
public class HostModel
{
//this works
[UIHint("FirstName")]
//this does not work
[UIHint("~/Views/Home/DisplayTemplates/FirstName.cshtml")]
public string Name { get; set; }
}
and yes
FirstName.cshtml is in HostApplication/Views/Home/DisplayTemplates/FirstName.cshtml
again sorry for the long post, but i gave up on finding a solution, so any help would be totally appreciated.
Danny is correct. The Templates are found the same way that Partial Views are found.
By default the WebFormViewEngine and RazorViewEngine are going to search the following locations for a template.
For display templates:
~/Views/{controller}/DisplayTemplates
~/Views/Shared/DisplayTemplates
For editor templates:
~/Views/{controller}/EditorTemplates
~/Views/Shared/EditorTemplates
I think the name of the sub-directories (i.e., "DisplayTemplates" and "EditorTemplates") are hard-coded into MVC somewhere (I know it's open source and I could find it, but I'm not going to).
I think the easiest way to change the location somewhat is to override the ViewEngine. My custom ViewEngine is pretty complicated at this point, but I suspect you could get away with the following.
Let's say you want your templates to be in ~/Views/Templates.
Create a class that inherits from the view engine you're using now (probably WebFormViewEngine or RazorViewEngine). Add an empty constructor. It should looks like this:
namespace MySite
{
public class MySiteViewEngine : RazorViewEngine // <<-- or WebFormViewEngine
{
public MySiteViewEngine()
{
// We'll put some code here in a later step
}
}
}
Now, add the following lines to the Application_Start method of Global.asax.cs:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MySiteViewEngine());
At this point, compile and run your application. Everything should be running exactly like it is running now. You're basically using the same view engine you were using before.
But now, we want to add a location to search when looking for PartialViews. This is simply done by adding to the PartialViewLocationFormats. So, now in the constructor of your custom view engine, you want to add to the base class' array like so:
base.PartialViewLocationFormats = new string[] {
"~/Views/Templates/{0}.cshtml"
}.Union(base.PartialViewLocationFormats).ToArray<string>();
A couple of notes about the above:
The entry above will make it so that your view engine looks for the String display template at ~/Views/Templates/DisplayTemplates/String.cshtml.
The location format in these view engines includes the file extension, so if you're using Razor/C# use "cshtml", Razor/VB use "vbhtml", WebForms add "aspx" and "ascx".
The way I'm doing it above, I'm adding my location format to the top of the list but keeping all the default locations. You might consider removing those.
Watch the current formats and you'll see that you will also get a controller in the {1} position in the format, so if you wanted to have a Templates directory underneath every controller you could.
Careful, once you get started moving things around with a view engine, it gets addictive. You might find yourself moving everything around.
Good luck.
Instead of creating a new ViewEngine you can easily modify the existing ones at runtime:
private void FixMvcTemplateAreaBug(string areaName)
{
foreach (BuildManagerViewEngine viewEngine in ViewEngines.Engines)
{
List<string> viewLocations =
new List<string>(viewEngine.PartialViewLocationFormats);
foreach (var extension in viewEngine.FileExtensions)
viewLocations.Add("~/Areas/" + areaName +
"/Views/Shared/{0}." + extension);
viewEngine.PartialViewLocationFormats = viewLocations.ToArray();
}
}
Place the above in an appropriate location (like area registration) and you'll be fine.
I think you've had no answers, because there isn't one :-(
I've searched high and low the last few days trying to find a solution to this (since as you mentioned, it's had a lot of views). Unfortunately I can't find any way to override this.
I think you're stuck :(
Edit: Came across this post for reading views from a database instead of the disk:
ASP.NET MVC load Razor view from database
I wonder whether the DisplayTemplates are read this way. If so, you could try hijacking them and reading them from another location (instead of DB)?
Pardon if I missed something above, but is there a reason that the override of Html.DisplayFor that allows the template to be specified won't work here? For example:
#Html.DisplayFor(m => m.MyItem, "~/Views/Controller/DisplayTemplates/MyItem.cshtml")
Cheers,
It might be a little late :) but this is now possible in MVC 4. The generic templates has to be placed in ~/Views/Shared/EditorTemplates
Source

Multilingual URLs with ASP.NET MVC

I’m working out the concepts for a new project where I need to support for multilingual URL’s. Ideally all URL’s need to be in the native language of the user. So we don’t want to use domain.com/en/contact and domain.com/es/contact but we like domain.com/contact and domain.com/contactar (contactar is Spanish for contact). Internally both should be routed to the same ContactController class.
This could be handled by adding multiple static routes to Global.asax.cs for each language but we’d like to make this very dynamic and would like the user of the system to be able to change the translation of the URL’s through the content management system. So we need some kind of dynamic mapping from URL’s to controllers and actions.
By looking at the source code of MVC3 I figured out that the ProcessRequestInit method of MvcHandler is responsible for determining which controller to create. It simply looks in the RouteData to get the name of the controller. One way to override the default MVC routing would be to create a simple default route that uses a custom RouteHandler. This RouteHandler forces MVC to use my own custom subclassed version of MvcHandler that overrides the ProcessRequestInit method. This overridden method insert my own dynamically found controller and action into the RouteData before calling back to the original ProcessRequestInit.
I’ve tried this:
Global.asax.cs
routes.Add(
new Route("{*url}", new MultilingualRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Default", action = "Default" })
}
);
MultilingualRouteHandler.cs
public class MultilingualRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MultilingualMVCHandler(requestContext);
}
}
MultilingualMvcHandler.cs
public class MultilingualMVCHandler : MvcHandler
{
public MultilingualMVCHandler(RequestContext context) : base(context)
{
}
protected override void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)
{
if (RequestContext.RouteData.Values.ContainsKey("controller"))
{
RequestContext.RouteData.Values.Remove("controller");
}
if (RequestContext.RouteData.Values.ContainsKey("action"))
{
RequestContext.RouteData.Values.Remove("action");
}
RequestContext.RouteData.Values.Add("controller", "Product");
RequestContext.RouteData.Values.Add("action", "Index");
base.ProcessRequestInit(httpContext, out controller, out factory);
}
}
In this handler I hardcoded the controller and action for testing purposes to some fixed values but it’s not difficult to make this dynamic. It works but the only problem is that I had to modify the source code of ASP.NET MVC3 to get it working. The problem is that the ProcessRequestInit method of MvcHandler is private and thus cannot be overridden. I’ve modified the source code and changed it to protected virtual which allows me to override it.
This is all great but possibly not the best solution. It’s cumbersome that I would always need to distribute my own version of System.Web.Mvc.dll. It would be much better that it would work with the RTM version.
Am I missing any other possibilities of hooking into ASP.NET MVC that would allow me to dynamically determine the controller and action to launch, depending on the URL? One other way I thought of is to build the RouteCollection dynamically on *Application_Start* but I think that will make it more difficult to change it on the fly.
I would appreciate any tips of hooks that I’ve not yet found.
This is fairly old now, nut just in case anyone else is looking for something similar...
Unless I'm completely misunderstanding what you want to do, it's pretty simple really.
Step 1: Add a new route to global.ascx.cs containing a reference to your personal routing engine
routes.Add(new MyProject.Routing.ContentRoutingEngine());
Make sure that it is in the right place in the list of routes so that other routing engines can catch stuff before it if required, or continue the route search if your engine doesn't handle a particular route. I put it after the ignores, but before the MVC default routes.
Step 2: Create the Content Routing Engine, making sure that it inherites from System.Web.Routing.RouteBase abstract class, and overrides the GetRouteData and GetVirtualPath methods as required e.g.
public class ContentRoutingEngine : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var routeHandler = new MvcRouteHandler();
var currentRoute = new Route("{controller}/{action}", routeHandler);
var routeData = new RouteData(currentRoute, routeHandler);
// set your values dynamically here
routeData.Values["controller"] = "Home" ;
// or
routeData.Values.Add("action", "Index");
// return the route, or null to have it passed to the next routing engine in the list
return routeData;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
//implement this to return url's for routes, or null to just pass it on
return null;
}
}
and that should do it. You can change routes as dynamically as you wish within your engine, and no changes to MVC source required. Let the standard MVC RouteHandler actually invoke the controller.
Postscript: Obviously the code above is not production standard - it's written to make it as obvious as possible what's going on.
If you are allowing modification of urls through your CMS, then you will have to keep all old versions of the urls so that you can 301 redirect to the new ones.
The best bet for this will be to put the url tokens eg "contactar" in the db along with its corresponding controller.
query that, and create your routes out of that.
create a route that will handle the 301s
I think that most elegant solution would be using some action filter combined with custom ActionInvoker. That way, you could invoke an action that has specific filters applied. Something like ActionName attribute, only capable to accept multiple values (names).
Edit: Take a look at ActionMethodSelectorAttribute, meybe you don't need a custom ActionInvoker after all.

Resources