Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
958 views
in Technique[技术] by (71.8m points)

asp.net mvc - Change lookup rule for views

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; 
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...