Try using a custom route. This is much more flexible than the alternatives presented here.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web;
using System.Web.Mvc;
public class ProductRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData result = null;
string virutalPath = httpContext.Request.Url.AbsolutePath.Substring(1).ToLowerInvariant();
// Call the database here to retrieve the productId based off of the virtualPath
var productId = Product.GetProductIdFromVirtualPath(virutalPath);
if (productId != Guid.Empty)
{
result = new RouteData(this, new MvcRouteHandler());
result.Values["controller"] = "Product";
result.Values["action"] = "Details";
result.Values["id"] = productId;
}
return result;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData result = null;
string controller = Convert.ToString(values["controller"]);
string action = Convert.ToString(values["action"]);
if (controller == "Product")
{
string path = string.Empty;
if (action == "Details")
{
Guid productId = (Guid)values["id"];
// Call the database here to get the Virtual Path
var virtualPath = Product.GetVirtualPathFromProductId(productId);
}
if (!String.IsNullOrEmpty(virtualPath))
{
result = new VirtualPathData(this, virtualPath);
}
}
return result;
}
}
You can use the route by adding it directly to your routes table in Global.asax, like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"home", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
// Add your custom route like so
routes.Add(new ProductRoute());
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Then on your localized product table, simply have a field that contains the path to lookup (without the leading slash). Of course, you didn't mention how you are storing your data, so you will have to come up with that on your own.
Also, you will need to handle your localization by parsing it out of the virtualPath. It SHOULD be there according to Google. You should handle the initial culture based on the headers passed and redirect (302) to the url of the culture that is selected. Then the user should be able to switch the language in which case you can put it in a cookie so their preference is remembered. However, search engines should be able to tell the culture from the URL without passing any headers.
This will handle the case for @Html.RenderAction() through the GetVirtualPath() method, and you can alter the logic if needed. I recommend you add caching because this will make every request hit the database as is. The route table is populated on app startup, but each route is executed on every request.
One more thing - to handle the "route doesn't match" scenario, simply return null and the router will move on to the next configured route. This allows you to configure as many custom routes as needed without mixing logic.