Change lookup rule for views

假如想象 提交于 2019-12-18 09:22:36

问题


I have an application that gets rolled out in multiple countries. There will be a setting in the web.config file, that defines the country. The country will not be in the URL.

Some of the the views change depending on the country. My first attempt is to use a folder inside the views folder that contains views, if they differ from the default view:

Default

/questions/ask.aspx

Spain

/questions/ESP/ask.aspx

If there is no view in the country-folder the default view is used. Is there a way to extend the ViewEngine to lookup views in the country folder first?

EDIT:

This is a poc only. To see a full implementation have a look at

http://pietschsoft.com/?tag=/mvc

      private static string[] LocalViewFormats = 

       new string[] {
           "~/Views/{1}/ESP/{0}.aspx",
        "~/Views/{1}/{0}.aspx",
        "~/Views/{1}/{0}.ascx",
        "~/Views/Shared/{0}.aspx",
        "~/Views/Shared/{0}.ascx"
    };

      public LocalizationWebFormViewEngine()
      {      
        ViewLocationFormats = LocalViewFormats; 
    }

回答1:


public class MyViewEngine : WebFormViewEngine
{
    private static string[] LocalViewFormats = new[] { "~/Views/ESP/{0}.aspx",
                                                          "~/Views/ESP/{0}.ascx" };
    public MyViewEngine()
    {
        ViewLocationFormats = LocalViewFormats.Union(ViewLocationFormats).ToArray();
    }
}

Obviously, you don't want to hardcode the location, but this should give you the general idea.



来源:https://stackoverflow.com/questions/639450/change-lookup-rule-for-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!