有没有人有任何使用ASP.NET MVC制作语言相关路线的经验?我想做的是有本地化的网址来改善SEO.那么例如 http://mysite.com/products/cars会映射到与 http://mysite.com/produkter/bilar相同的控制器/动作吗
我试过浏览一下,但我找不到类似的东西.我甚至不相信它真的是一个好主意,但我想,当用户用他们自己的语言进行搜索时,它会帮助搜索引擎优化.我想这会需要一些mvc路由引擎的定制.
编辑:到目前为止,Mato肯定有最好的解决方案,我希望看到一个Custom RouteHandler解决方案,以供参考.
您可以实现一个自己的控制器工厂类,在初始化之前转换控制器名称.例如,您可以将翻译存储在资源文件或DB中.最简单的方法是从DefaultControllerFactory继承并覆盖CreateController函数.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System.Web.Mvc
{
class CustomControllerFactory : DefaultControllerFactory
{
public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
/**
* here comes your code for translating the controller name
**/
return base.CreateController(requestContext, controllerName);
}
}
}
最后一步是在应用程序启动时注册控制器工厂实现(在Global.asax中).
namespace MyApplication
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(typeof(CustomControllerFactory));
}
}
}
