This project is read-only.

LoadTemplates and AddResources for VB.Net

Topics: faq
Apr 3, 2010 at 5:27 AM
Edited Apr 3, 2010 at 5:28 AM

I know this is quite against the C# momentum but I have a massive existing VB.Net application and would like to add in the C# WebServer component to serve the database back to remote users.

I have successfully got the server running with VB.net but It just won't load templates or resources for MVC. This is to do with the way VB.Net handles resources, as opposed to the way that C# handles them.

Can anyone offer an alternative to embedded Views and Public folder? Preferably for VB.net. A C# code example will do though.

I currently use Xenocode to wrap the exe up so its not a problem that the views and public are in the bin folder.

The idea is to get the Views and Public folder referenced locally instead of being embedded into the exe.

 

Thankyou,

Jeremy.


ps. This project is amazing.

Apr 3, 2010 at 7:51 AM

Version 1.1 or Version 2.0? They handle resources/files diffrently.

Apr 3, 2010 at 7:59 AM

Well for instance:

' since we do not use files on disk, we'll just add the resource template loader.
            Dim templateLoader As New ResourceTemplateLoader()
            templateLoader.LoadTemplates("/", Assembly.GetExecutingAssembly(), "ForgeApp.views")
            Dim templateManager As New TemplateManager(templateLoader)
            templateManager.AddType(GetType(WebHelper))
            templateManager.Add("haml", New HamlGenerator())

In VB.net resources are accessed via My.Resources .

Is there a way that I can just link my haml and views to files not in my solution? Ie files that are inside my project bin/exe folder?

Jeremy.

Apr 3, 2010 at 9:06 AM

Try the FileTemplateLoader

Apr 3, 2010 at 9:18 AM

Thankyou. Sorry to bother but...

Is there an example of its usage?

What about the public folder?

Apr 3, 2010 at 9:27 AM
Edited Apr 3, 2010 at 9:51 AM

Is this the correct usage?

' since we do not use files on disk, we'll just add the resource template loader.

            Dim TemplateLocation As String = Environment.CurrentDirectory & "\views\"
            Dim FileLocation As String = Environment.CurrentDirectory & "\public\"

            Dim templateLoader As New FileTemplateLoader("..\\..\\views\\")
            Dim templateManager As New TemplateManager(templateLoader)
            templateManager.AddType(GetType(WebHelper))
            templateManager.Add("haml", New HamlGenerator())

            ' we've just one controller. Add it.
            Dim controllerModule As New ControllerModule()
            controllerModule.Add(New UserController(templateManager, _language))
            _server.Add(controllerModule)

            ' add file module, to be able to handle files
            Dim fileModule As New FileModule("/", FileLocation, True)
            fileModule.AddDefaultMimeTypes()
            _server.Add(fileModule)

 

As I still get "InternalServerError: Controller failure, please try again."  Files load but the templates can not be found (assume).

Apr 3, 2010 at 1:16 PM
Edited Apr 3, 2010 at 1:16 PM

The path is relative to the Enviroment.CurrentDirectory, you can also use a absolute path. The view engine always uses controller name as a sub directory. i.e. if your controller name is "user", the engine will look in "[yourspecifiedpath]\\[controllername]\\[viewname].*"

Where is your views located?

And your static files?

 

Apr 3, 2010 at 1:49 PM

Static files are in folder 'public' and views in 'views'. These folders are then placed in the same directory as the application exe. ie:

bin\release\

                  views\user\   (etc)

                  public\

                  my.exe

 

Does the template manager have to be 'applied/added' to the _server ? ie _server.Add(TemplateManager)  or something?  I have this project working in C# which is why I am pulling my hair out trying to get it to work in VB. I'll donate some money to the project link for the prompt support you're giving.

Apr 3, 2010 at 8:32 PM

No, it should be enough to just add it to the controller.

 

"..\\..\\views\\" should be used if you have the files in your base project folder. "views\\" if it's in your bin\release\views folder.