Load view from bin folder?

Load view from bin folder?

终陌 发布于 2021-11-25 字数 1384 浏览 812 回复 2 原文

In an attempt to have several sites with one central set of views I created an external library with my views. They are marked as content and copy always.
This views library is referenced in my websites and thus with compiling get a "Views" folder in their bin folder.
Then I made a quick custom ViewEngine like this:

public class CommonViewLocator: WebFormViewEngine
{
    public CommonViewLocator()
    {
        MasterLocationFormats = new[] {  
            "~/bin/Views/{1}/{0}.master",  
            "~/bin/Views/Shared/{0}.master"  
        };
        ViewLocationFormats = new[] {  
            "~/bin/Views/{1}/{0}.aspx",  
            "~/bin/Views/{1}/{0}.ascx",  
            "~/bin/Views/Shared/{0}.aspx",  
            "~/bin/Views/Shared/{0}.ascx"  
        };
        PartialViewLocationFormats = ViewLocationFormats;  
    }
}

After running I get this screen:

The view 'Index' or its master could not be found. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/bin/Views/Home/Index.aspx
~/bin/Views/Home/Index.ascx
~/bin/Views/Shared/Index.aspx
~/bin/Views/Shared/Index.ascx

What's going wrong?
Also, it feels kinda weird that the bin folder has the views, any other suggestions are welcome.

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

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

评论(2

爱情眠于流年 2022-06-07 2 楼

If you are absolutely sure the content is in the right folder, then it's probably the server that doesn't let anything to be served from the bin directory, for obvious security reasons.

If that's the case, what I suggest is that you keep the views in the external project, then create HTML extension methods that render those views.

三生池水覆流年 2022-06-07 1 楼

Why not use a post-build action on the projects to move the files in /bin/Views into /Views? Then you wouldn't have to do anything special in code.