开发者

ASP.NET MVC view locations and routing

I have a base controller that I use to return basic views like this.

  public ActionRes开发者_Python百科ult Index(string pageName)
        {
            return View(pageName);
        }

        public ActionResult LanguageSpecific(string ul, string pageName)
        {
            var result = View("sv/" + pageName);

            return View(result.ViewName);
        }

The controller's name is home is there a way that for it not to look for the sv content in /home but just in /sv

  "EnglishRoute",                                              // Route name
                "{pageName}.aspx",                           // URL with parameters
                new { controller = "Home", action = "Index", pageName = "" }  // Parameter defaults
            );

            routes.MapRoute(
              "SwedishRoute",                                              // Route name
              "{ul}/{pageName}.aspx",                           // URL with parameters
              new { controller = "Home", action = "LanguageSpecific", ul = "",pageName = "" }  // Parameter defaults
          );

It looks in these locations:

  • ~/Views/Home/sv/index.aspx
  • ~/Views/Home/sv/index.ascx


When you call the View method you can pass in an app-relative path that starts with "~/" and then ASP.NET MVC will use the exact path you specify:

return View("~/UseExactlyThisFile.aspx");

That way it won't do its search in the various paths and locations that are pre-configured.

Please keep in mind that this doesn't have very much to do with routing (though it does a little bit).


If you try to localize your pages, why don't you use resources? With the pattern above you don't really take the advantages of mvc. Or do i misunderstand you? A simple solution would be to use an action filter which picks up the language identifier from the route and sets the UICulture. The Views then may use resources to localize their content.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜