How to make intellisense works with RazorEngine? - c#

I am trying to configure RazorEngine so that intellisense works on views. I add RazorEngine and Microsoft.AspNet.Mvc using nuget. I create TestView.cshtml and declare #model MyModel but it says The name 'model' does not exist in the current context. I also cannot use intellisense inside the view.
Do I miss any step here? How to enable intellisense in the View?

You can use
#using RazorEngine.Templating
#using Namespace.Of.My.Model
#inherits TemplateBase<MyModel>
on the top of your template.
This works fine on a new Console Application with Visual Studio 2013 (after adding a reference to RazorEngine).
The documentation for this is here.
EDIT:
I noticed that this only works when the RazorEngine project is added to the solution and directly referenced. If you use the NuGet package you additionally need to ensure one of the following to make it work:
Your project output path is set to bin\ instead of bin\Debug\ and bin\Release\.
Copy RazorEngine.dll and System.Web.Razor.dll to bin\

I know this question is kind of old. I could not get anything to work, no matter the solution.
I have a hack fix that may be palatable to some. I don't like it very much, but it's the most usable thing I've gotten so far.
The trick is to define the "Model" yourself as a variable from the actual Model. I defined it as "TrueModel", but whatever name you can think of that doesn't collide with "model" or "Model" should work. Then just replace all of your instances of "Model" with "TrueModel".
#using Namespace.To.My.Models
#* This line should still look like an error,
but we only really care about the intellisense in the rest of the .cshtml file. *#
#{ ModelType TrueModel = (ModelType)Model; }
<div>
#TrueModel.MyProperty is here now.
</div>
<p> #TrueModel.MyOtherProperty is great! </p>
It's not a great solution, but it might be useful.

Oh, I faced with such problem while adding Razor Engine to my custom dll project. To solve this you have to:
1.correctly setup namespaces in web config file(hope you have it in views folder, if not - copy from MVC project):
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.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" />
<add namespace="System.Web.Optimization" />
</namespaces>
</pages>
</system.web.webPages.razor>
...
2.use to build into bin\ path(not any other, you may use copy post-build command to move results to another place)
3.clean solution and remove obj and bin folders, than build
My views' code starts from #model MyModelClass and all works fine

Related

How can i write a custom html helpers with suggestions for parameter [duplicate]

I can't get intellisense for my own html helpers. My CustomHtmlHelpers.cs looks like this:
using System.Web.Mvc;
using System.Text;
using System.Web;
namespace laget.Web.Helpers
{
public static class CustomHtmlHelpers
{
//MY HELPERS
}
}
and in my Web.config:
<pages>
<namespaces>
<add namespace="laget.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
<add namespace="System.Web.Helpers" />
</namespaces>
</pages>
If I put <#using laget.Web.Helpers> in my view, I get the intellisense issue fixed.
Should it not be enough with the code in Web.config?
Sometimes it doesn't seem to work right away. Try closing the .cshtml file, and re-opening it. Then if that doesn't work, try restarting Visual Studio. Also make sure you actually compiled your project, intellisense won't work with non-compiled helpers.
I'm pretty sure that you're not editing the correct Web.config file.
You need to add your namespace to the one in your Views directory.
<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" />
<add namespace="laget.Web.Helpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
You actually don't need to restart Visual Studio in most cases. All you need to do is close the .cshtml file and reopen it!
It needs it on the local page. I'm pretty sure this has to do with Namespace resolution. It isn't exactly sure what you are referring to without the local using statement.
I ran into this today as well. Sometimes just closing the Razor view's window in Visual Studio and re-opening it will do the trick without having to do a full Visual Studio restart.
I tried to solve an issue like this one yesterday. I had e pre-compiled dll (project name ie: MyHtmlHelpers) containing helpers and lot of other classes.
I had the assembly referenced in the web project and the all "standard"-helpers showed up in intellisense but, even though I added the namespace to both web.config in the root and in the views-folder nothing worked. When running the project helpers works, but not in intellisense.
I added a new class and wrote a new html helper inside the web project, added the namespace to web.config. And that worked.
After some hours add tried my last card, adding the MyHtmlHelpers-project to the same solution as my webproject. That did the trick. I diden't change anything in the configs just added the project to the same solution and changed the reference to point at the project insted of the compiled dll.
Isen't that strange? A VS-bug?
I found that i was adding the reference to the wrong web.config. It's not the main config but the web.config in the views directory...
So now I will show you the steps
1.Create or open an existing class library project (if you open an existing one be sure to remove the MVC5 nuget package)
2.Add the MVC (5.0) nuget package (
right click project in solution explorer -> Manage NuGet Packages -> search for MVC and install “Microsoft ASP.NET MVC”)
3.Close any and all open .cshtml files
4.Right click project -> Properties -> Build -> change Output path to your project “bin/”
5.Add the following minimal Web.config to the root of your class library project
( the web config file is solely needed for intellisense. Configuration (via Web.config)
should be done in the WebApplication hosting your ClassLibrary assembly)
6.Clean and Build the solution.
7.Open cshtml file and try now :)
I found that if it still doesn't work, you may need to go to the properties of the custom class and change the build action from "content" to "compile". That resolved it for me.
I try all of this solutions, one more thing which i didnt find is that in root web.config i must change webpages:Version from 2.0.0.0 to 3.0.0.0.
Open and close all .cshtml files and it's worked.
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />

Telerik namespace cannot be resolved in ASP.NET MVC Razor view

I'm trying to render Telerik Reporting (Q2 2014 SP1 - trial version) in my ASP.NET MVC 4 application (which uses .NET 4.5 framework), but for some reason "Telerik." namespace cannot be resolved in ASP.NET MVC Razor view, although I can access "Telerik." namespace in controller.
Razor view:
#using Telerik - error is in here
#Html.TelerikReporting() - error is in here
Web.config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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" />
<add namespace="Telerik.Reporting" />
<add namespace="Telerik.ReportViewer.Mvc" />
</namespaces>
</pages>
</system.web.webPages.razor>
Telerik libraries my ASP.NET MVC project is referencing:
Telerik.Reporting
Telerik.ReportViewer.Mvc
What can be the issue with my set up?
I solved my problem.
I've been desperately trying following approaches: running solution in VS2012, deleted bin, obj folders, moving libraries from one path to another, restarting machine and VS numerous times and everything was futile.
However I've noticed that Telerik.Reporting.dll and Telerik.ReportViewer.Mvc.dll were not getting copied in my bin folder. In Visual Studio I expanded "References", clicked on each dll file and went to each dll's "Properties" and toggled "Copy Local" property from "False" to "True".
I cleaned and rebuilt my MVC project, checked that the Telerik dll files appeared in bin folder, went to my Razor view and #Html.TelerikReporting() worked!
Out of curiosity I toggled "Copy Local" property from "True" to "False", repeated the rest of the process and #Html.TelerikReporting() still worked. I don't know if there is a problem with Visual Studio or my "Professional" installation.
You shouldn't need a
#using Telerik
if the appropriate namespaces are imported in your web.config. Sometimes in order for your Razer views to pick up on the web.config namespaces after being initially added, you need to restart visual studio for some reason.
I would validate that "Telerik" is the appropriate namespace for the TelerikReporting method you're trying to access.
Additionally, you have a syntax error in:
#Html.TelerikReporting()
It should (likely) be
#(Html.TelerikReporting())

Razor - Importing Namespaces that Can't Be Found

I am currently working with a solution that contains a C# project and a Razor web page project. I am looking to import a namespace from that C# project to the web page but it gives an error saying that the namespace could not be found. The namespaces in the project are structured "CompanyName.WordThatDescribesNamespace". When I say #using Company.Namespace it says that there is no namespace called Company. Is using that period preventing it from importing the namespace or is there a way around it?
From your question I understood this following this.
You have two projects in a solution
One is a class project (dll) and another one is an MVC project.
You need to access the methods in class project to the MVC.
Please make sure you did the following things
1. Added reference to the dll of the class project having CompanyName.WordThatDescribesNamespace. You can got to Solution Explorer and verify this.
2. If referred , please check the properties, whether you set CopyLocal=True. Deletes the dlls in Bin folder and try to refer and changing it to False and build the project.
3. You can also add namespace to the Web.config residing in the Views folder, so it will be available for all views.
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
...
<add namespace="Infragistics.Web.Mvc" />
</namespaces>
</pages>
Hope any one of the above helps you.

How to avoid having to use fully qualified name for the Styles.Render method in a Razor view

I'm trying to implement the Styles.Render method in a project of mine and I am using the separate, pre-built MVC 4 internet application project that's created from the New Project wizard as my model. In the pre-built MVC website's _Layout.cshtml layout view, the following code appears within the head tag:
#Styles.Render("~/Content/css")
However, in my own project, just using the #Styles.Render syntax does not seem to work. For some reason, the view does not like it. And I have to resort to using the fully qualified version of the method:
#System.Web.Optmization.Styles.Render("~/Content/css")
I would like to know what am I missing or how can I avoid having to use the fully qualified type and member syntax from within a Razor view in the manner of the pre-built version as described above? Thanks.
You're missing the System.Web.Optimization namespace reference in the web.config in the views folder
<configuration>
<system.web.webPages.razor>
<pages basePageType="...">
<namespaces>
<add namespace="System.Web.Optimization"/>
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>

C# ASP.NET cs0246 issue

I'm having an issue where I keep getting error message CS0246 on my web server, but not in my visual studio environment. The issue is coming from a dll I reference in my project. The server cannot find my namespace for my dll.
To solve this I tried copying every file in my project directory to my web server, adding a tag in my web.config file, and adding an import statement in my aspx page. I have also tried adding an assembly reference in my aspx page, but that seems to require a change in the registry which I don't think I have access to.
I should also note my dll and namespace name are different, but changing the names of either did nothing. I also completely removed the dll and copied the code files directly into my project, but it still could not find the namespace.
I thus think I'm supposed to register the namespace and dll somewhere else, but I cannot figure out where. Any help will be appreciated.
If it matters, both my web project and dll are using .NET2.0
I know this is the equivalent of the annoying "did you reboot", but have you tried making a new solution and re-adding the projects and references? Sometimes the .sln file can get corrupted.
Sometimes, the web.config file contains instructions for the .NET framework to generate temporary files.
Please check your config file for something similar to:
<system.web.webPages.razor>
<host
factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.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" />
<add namespace="YourNamespace.ShouldBeCorrect.Here" />
</namespaces>
</pages>
</system.web.webPages.razor>
This is likely the case if your error page contains a reference to a dynamically generated file such as:
Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\133f2363\4ba6d0ba\App_Web_index.cshtml.a8d08dba.ociepbng.0.cs Line: 27

Categories