adding namespaces when using RazorEngine - c#

I am currently using the RazorEngine library to create html Email templates. Code:
var result = Engine.Razor.RunCompile(File.ReadAllText(templateFilePath), key, typeof(T), data);
Within the template itself I want to get access to some of the helper methods provided by MVC, namely #Html.Format(string string);
However Whenever I add said line of code in the template is:
Errors while compiling a Template.
Please try the following to solve the situation: * If the problem is
about missing/invalid references or multiple defines either try to
load
the missing references manually (in the compiling appdomain!) or
Specify your references manually by providing your own IReferenceResolver implementation.
See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
Currently all references have to be available as files! * If you get 'class' does not contain a definition for 'member':
try another modelType (for example 'null' to make the model dynamic).
NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
Or try to use static instead of anonymous/dynamic types. More details about the error:
- error: (180, 97) The name 'Html' does not exist in the current context
Which does explain that I need to reference the right namespace. Question is, what do I reference and where?

Are you looking for HtmlHelper in System.Web.Mvc ?
Ref: https://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper_methods(v=vs.118).aspx

Related

Visual Studio Templates - custom parameters

I am using VS 2015 templates to create a project for me, within my C# classes I have substitutions like $projectname$ and they work great if I name my project like this - MyTemplatedProject. I use $projectname$ for the part of class names and namespaces.
If I create generate a class like $projectname$Context, it becomes MyTemplatedProjectContext and all is well.
But if I name my project MyTemplatedProject.FrontEnd I have problems with the classes that are generated because they have the . in their name.
The substitution $projectname$Context becomes MyTemplatedProject.FrontEndContext and that does not work for a class name.
How do I create custom parameters based $projectname$, ideally I would have a parameter like $projectnameUpToFirstDot$ which only returns MyTemplatedProject when the project name is MyTemplatedProject.FrontEndContext?
I had exactly the same problem a week ago. I just needed some customed variables inside of my template, which would resolve in $projectname$ upper case and one other in lower case. Things like that.
It turned out for me to be the most effective and flexible way to develop my own project wizard using VS' custom wizard template.
Unless JavaScript & HTML is not a no-go for you, it is pretty easy to have some string manipulation on variables like $projectname$ there.
Once you set it up, go in default.js and edit in function
function onFinish()
{
...
var prjName = wizard.FindSymbol('PROJECT_NAME');
wizard.AddSymbol('PROJECT_NAME_WITHOUT_DOTS', prjName.split('.')[0]);
...
}
split('.') removes all dots and devides it into an array of strings. With [0] you select just the part up to the first dot.
In your template files you can then address the symbol PROJECT_NAME_WITHOUT_DOTS with the following syntax:
class [!output PROJECT_NAME_WITHOUT_DOTS] {
...
}
If you just want to generate a valid class name based on the project name, instead of creating a custom template parameter, you can change $projectname$ to $safeprojectname$, which is documented as:
The name provided by the user in the New Project dialog box, with all unsafe characters and spaces removed.

Definition.Entity property is null

Whenever I create a ReloadableObjectRegistry with path to directory containing compiled modules, Definitions in the tree have Entity property set to null. The reason why I need to access this property is to be able to read the syntax of a definition.
I suspect, the Entity property only gets set after parsing a MIB... Is this a correct assumption? How else can I find out definition's syntax? The Type property is always 'Unknown'.
Sample code:
private ReloadableObjectRegistry Objects;
Objects = new ReloadableObjectRegistry(#"some_path");
P.S. By the looks of it, parsed module (.module) does not have any information about MIB Types in it.
The open source edition SharpSnmpLib.Mib was designed that way, where most of the classes are just placeholders.
If you do need to learn the syntax, you have to use SharpSnmpPro.Mib at http://sharpsnmp.com.
A sample project has been published on GitHub to demonstrate usage of the new APIs.

Alias resource file namespace in C#

I have an ASP.net MVC 3 project with resource files setup in folders like:
/Resources/EntityName/Views/
/Resources/EntityName/Models/
This means the namespace to access the strongly typed resource values is:
Resources.EntityName.Models.ModelA.Property1
Visual studio gives a compile time error if I try to include the namespace "resources.xxx" and it won't allow the using alias syntax either.
Is there anyway to include or atleast alias the strongly typed namespace of a resource file like it was a normal namespace?
There should be no reason why you can't use one of these objects within a class:
using YourProject.Resources.EntityName.Models;
You might try something like this (not tested, just an idea)
public class myModelA : Resources.EntityName.Models.ModelA
{ /*Leave empty here, nothing to do*/ }
Then you may be able to call the shortest myModelA instead of the complete, verbose name of the resource. Watch out because you'll not be able to access private members of your original model if you inherit it like thi.

The model item passed into the dictionary is of type 'MyType', but this dictionary requires a model item of type 'MyType'

Yes, I went through the questions with similar titles. None of them seem to match the situation I'm in. It's simply the programmer sending a wrong object type to the view.
This is the exact error message I'm receiving:
The model item passed into the dictionary is of type 'IS.Extensions.Models.ContactSelectList', but this dictionary requires a model item of type 'IS.Extensions.Models.ContactSelectList'.
I started moving around views and model classes in our solution in an attempt to make it 'framework-like'. With this I mean that some views/models we have are used in multiple MVC applications and some views/models are specific for a certain MVC application.
The issue started áfter moving the ContactSelectList view.
Common views are in a project that uses the Razor generator to compile them.
I have this in my view:
#model IS.Extensions.Models.ContactSelectList
and when debugging I can see that the model I'm sending to the RenderPartial method is of THE SAME TYPE:
Some sidenotes:
IS is a MVC web app and references IS.Extensions
IS.Extensions is a class library referenced by IS
ContactSelectList.cshtml is located in IS\Views\Controls
The ContactSelectList.cs model class is located in IS.Extensions\Models
The 'parent view' (the view that triggers this RenderPartial call is situated in the 'common' project: ZModel.Web\Views\Controls
Does anyone have any idea what's going on here? The error messages is kinda confusing and not really a help..
Edit after Erics comment:
The assembly containing the model class (ContactSelectList.cs) is dynamically loaded. Why? Because I thought it makes sense to create a kind of extension / plugin system, where dropping a certain dll in a certain directory extends the common models with some application specific ones.
This is how (slightly modified):
var zExtenderAssembly = Assembly.LoadFile(path);
extenderType = zExtenderAssembly.GetTypes().ToList().Where(t => t.GetInterface("IZExtender") != null).FirstOrDefault();
return (Activator.CreateInstance(extenderType) as IZExtender).CreateBO("ContactSelectList");
The 'CreateBo' method in the ZExtender (implements IZExtender) simply creates a new instance of a ContactSelectList (which is in the same library as the ZExtender, so no reflection needed):
public Component CreateBO(string name)
{
...
return new ContactSelectList();
...
}
I moved the extension dll to the bin directory of the web application and changed the way the dll is loaded.
I used to load it by specifying the full path to the .dll file. Loading the extension dll using Assembly.Load('myAssemblyName'), made everything work as before.

Problem with doxygen references listing

I'm experimenting with using doxygen on a C# application, however I'm having issues with the REFERENCES_RELATION option. For example, I have the following method:
class ControlManager
{
...
public void SendRequest(IRequest req)
{
Log.WriteLine("Received request {0}", req.Name);
requestChain.Handle(req);
}
private MessageChain<IRequest> requestChain = ...;
}
Which generates the following references line in the html docs:
"References IRequest.Name, ControlManager.requestChain, and Log.WriteLine()."
What I really want it to show is:
"References IRequest.Name, MessageChain.Handle() and Log.Writeline()."
It seems that as Doxygen doesn't see it as a method reference, which means that the MessageChain.Handle docs also doesn't show it as referenced by anything (using the REFERENCED_BY_RELATION option). Is there any way of doing this, or have I missed some key reason why the class field is treated differently from the method parameter?
I'm not sure if it matters but ControlManager and MessageChain are in different namespaces and different projects.
ControlManager and MessageChain are in different namespaces and different projects.
Do MessageChain and ControlManager classes are parsed using the same doxygen project file ?
If not, as doxygen can't generate a documentation for one of your class, the reference won't be added.

Categories

Resources