Razor Tips and Tricks #3 - Look, No forms

Category: ASP.NET DotNetNuke
Last Modified: May 2 2017
Dec 10 2010

On Monday, we launched the Razor DotNetNuke Hackathon with a meeting of SEADUG (Seattle DotNetNuke User Group) held at Microsoft offices in beautiful downtown Bellevue.

For those of you proposing to write Razor “scripts” as part of the Hackathon, I have decided to write a series of short Tips and Tricks.

In this third post I will cover a couple of caveats when using Razor scripts in the DotNetNuke context.

No Forms

The first caveat is that because the Razor script is still running within the context of a ASP.NET WebForm, you need to be careful when using Razor scripts that you find (or write yourself) for collecting form data. 

tags cannot be embedded inside other form tags – and because an ASP.NET WebForm (.aspx) always contain a tag, this means that Razor scripts used in the DotNetNuke context cannot contain tags.

The workaround is simple – just remove the tags from any Razor script you use in DotNetNuke.  Any submit button will post back as usual and DotNetNuke will pass the Form variables to the script as normal.

Urls

The second caveat is that you should be careful with following Razor script examples that use Urls.  For example you will often see something like:

@RenderPage("/Shared/_Header.cshtml");

The initial “/” means that we the url is starting at the web-root.  As most Razor examples you see will assume that your Razor script is executing in the context of web-root – this example is fine.  But in DotNetNuke, the Shared directory would probably be buried deep in the website folder structure (for example ~/DesktopModules/RazorModules/MyModule/Shared/).

In this case just leave out the initial “/” so that the url is relative.

@RenderPage("Shared/_Header.cshtml");

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Tags