Forum


HomeHomePremiumPremiumDevelopmentDevelopmentUsing Razor from within a DNN web forms moduleUsing Razor from within a DNN web forms module
Previous
 
Next
New Post
5/18/2015 9:39 AM
 

Hello, 

Has anyone seen an up to date example showing how to use Razor from within a DNN Web Forms module, I assume this is possible?

Just a simple example would do. Something that shows a template and the code to do the merge and write the result to a page.

Steve

 
New Post
5/19/2015 10:38 AM
 

Hello,

To follow up, I've tried the following from this article:
http://maxiomtech.com/cms/stop-using-tokens-just-use-razor/

 

I've referenced DotNetNuke.Web.Razor.dll

I created a class:

        public class MyClass
        {
            public string MyProperty { get; set; }
        }

Then included the following code:
    string templatePath = ControlPath + "Templates/Example.cshtml";
    var razorEngine = new DotNetNuke.Web.Razor.RazorEngine(templatePath, ModuleContext, LocalResourceFile);
    var writer = new StringWriter();
    razorEngine.Render(writer, new MyClass {MyProperty = "Fred"});
    placeHolderOutput.Controls.Add(new LiteralControl(Server.HtmlDecode(writer.ToString())));


But what to write in the Razor file 'Example.cshtml'?

I can only get plain text written into the page.


 

As the article states, I've tried inherits but the class isn't recognised:

@inherits DotNetNuke.Web.Razor.DotNetNukeWebPage<MyClass>

 

Can anyone please put me out of my misery :-)

 

Steve

 
New Post
5/20/2015 5:39 AM
 

Hello,

 

Finally, I got this working, the Razor script required the following:

@inherits DotNetNuke.Web.Razor.DotNetNukeWebPage<MyNameSpace.MyClass>
@Model.MyProperty

 

Had to include the full namespace before the class and use Model with a capital 'M'.

So since this works inside an ascx control, it will allow a step by step migration from web forms to Razor.

 

Steve

 
New Post
5/20/2015 8:29 AM
 

Steve,

Good to hear you figured it out.

Since DNN has already figured out how to implement Razor in a usercontrol for their Module Builder code, you can see how they do it.  If you download the DNN source and look in the DNN Platform/Modules/DotNetNuke.Modules.RazorHost project, you can see RazorHost.ascx that loads the razor script into the module view.

 
New Post
5/20/2015 9:27 AM
 

Hello Aderson,

 

Thank you for your reply.

 

Yes this would have been a better approach. Next time!

 

Best Regards,

 

Steve

 
Previous
 
Next
HomeHomePremiumPremiumDevelopmentDevelopmentUsing Razor from within a DNN web forms moduleUsing Razor from within a DNN web forms module



Try FREE
30 days money back guaranteed