更改视图的查找规则

发布于 2024-07-15 03:28:49 字数 914 浏览 7 评论 0原文

我有一个在多个国家/地区推出的应用程序。 web.config 文件中有一个定义国家/地区的设置。 该国家/地区不会出现在 URL 中。

有些观点因国家/地区而异。 我的第一次尝试是使用包含视图的视图文件夹内的文件夹(如果它们与默认视图不同):

Default

/questions/ask.aspx

Spain

/questions/ESP /ask.aspx

如果国家/地区文件夹中没有视图,则使用默认视图。 有没有办法扩展 ViewEngine 以首先查找国家/地区文件夹中的视图?

编辑:

这只是一个 poc。 要查看完整的实现,请查看

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; 
    }

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; 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2024-07-22 03:28:49
public class MyViewEngine : WebFormViewEngine
{
    private static string[] LocalViewFormats = new[] { "~/Views/ESP/{0}.aspx",
                                                          "~/Views/ESP/{0}.ascx" };
    public MyViewEngine()
    {
        ViewLocationFormats = LocalViewFormats.Union(ViewLocationFormats).ToArray();
    }
}

显然,您不想对位置进行硬编码,但这应该可以让您了解总体情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文