Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Razor without Visual Studio? System.Web.WebPages.Html.HtmlHelper vs. System.Web.Mvc.HtmlHelper

I am trying to create a website using some of the Razor functionality, but without using Visual Studio. The main reason for this is that I'm currently mainly working from a Mac, and I'd like to use Coda for editing the pages.

I don't need a programmable website, in the sense of database access, that sort of thing, but I'd like to use RenderPartial so that I can split up the pages and implement some common functionality, like menus and so on.

So, I tried the following:

  1. I created a new site under IIS7
  2. I dropped in a .cshtml file with this code: @DateTime.Now
  3. I opened it through Chrome

This displayed the current date and time, so clearly the razor engine executes.

Then I tried the following code:

@Html.RenderPartial("menu.cshtml")

and I get this error message:

c:\inetpub\drip\dev2\index.cshtml(1,12): error CS1061: 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'RenderPartial' and no extension method 'RenderPartial' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

Clearly, the identifier Html refers to a different class than I expected.

So, is there a tutorial on how to do this? Is it at all possible?

This code:

@Html.GetType().FullName

Outputs this:

System.Web.WebPages.Html.HtmlHelper

I don't need to use Html as the identifier, if I can create my own variable referring to the right object/type/class instance, that'd be more than enough for me.

like image 746
Lasse V. Karlsen Avatar asked Apr 01 '26 17:04

Lasse V. Karlsen


1 Answers

Clearly, the identifier Html refers to a different class than I expected.

Yes, it refers to System.Web.WebPages.Html.HtmlHelper which is used by WebPages. In ASP.NET MVC it is the System.Web.Mvc.HtmlHelper class.

The base class of your template is the WebPage class whereas in ASP.NET MVC it is the WebViewPage class.

But since you are using WebPages you could rely only on the methods defined by it. For example if you wanted to include a partial you could do the following:

~/_SiteLayout.cshtml:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>test</title>
    </head>
    <body>
        @RenderBody()
    </body>
</html>

~/Default.cshtml:

@{  
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Welcome to my Web Site!";
}
<div>Welcome</div>

@RenderPage("Menu.cshtml")

~/Menu.cshtml:

@{  
    Layout = null;
}
<div>Some menu</div>

web.config:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
</configuration>

You may take a look at the WebPages documentation. There are lots of tutorials out there.

like image 74
Darin Dimitrov Avatar answered Apr 03 '26 08:04

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!