如何在IControllerFactory中更改网址

时间:2019-05-30 13:20:56

标签: c# asp.net-mvc

我正在处理一项任务,例如当您转到网址:/ Home / Index, 并将您的浏览器语言设置为英语,则应将其重定向到/ Home_en / Index

目前,我设法从Home_en控制器查看索引,但网址保持不变-/ Home / Index

我还在Globas.asax中添加了一行:

ControllerBuilder.Current.SetControllerFactory(new ControllerFactoryController());

 public IController CreateController(RequestContext requestContext, string 
      controllerName)

        Type targetType = null;
        string[] languages = HttpContext.Current.Request.UserLanguages;



        if (languages[0] == "en-US")
        {
            requestContext.RouteData.Values["controller"] = "Home_en";
            targetType = typeof(Home_enController);

        }
        else
        {
            requestContext.RouteData.Values["controller"] = "Home_pl";
            targetType = typeof(Home_plController);

        }

        IController controller = Activator.CreateInstance(targetType) as IController;

        return controller;
    }

如何在网址中将其显示为/ Home_zh / Index

0 个答案:

没有答案