Reputation: 391276
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:
@DateTime.Now
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.
Upvotes: 3
Views: 5017
Reputation: 1038710
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.
Upvotes: 4
Reputation: 6911
You seem to need to reference System.Web.Mvc.Html
in your web.config to be able to use extension methods from RenderPartialExtensions class
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
Upvotes: 0