简体   繁体   中英

Load view from bin folder?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM