question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting base type and setting namespaces

See original GitHub issue

Hi again. I’m trying to replace RazorEngine with RazorLight in my FormFactory project, but am getting some errors when I run my templates.

My templates are very similar to MVC views (in fact they are MVC views when I run them inside MVC, but I read them and change them when running as standalone templates), e.g. one might look like this:

@using System.ComponentModel.DataAnnotations
@using System.Linq
@using System.Xml.Linq
@using FormFactory  

@{
    @Model.GetCustomAttribute<DisplayAttribute>().Name
    var value = Model.Value as XElement;
    if (value != null) { 
        @Html.Raw(value.ToString())
    }
}

So I have using statements, and am using @model and Model and also Html.

I got this to work with RazorEngine by making the template inherit from a base type with Html and Model on it. How can I set the base type with RazorEngine? It looks like Model is set from the .Parse(..., model) but that won’t work for Html

-Also I’m getting errors like - The type or namespace name 'DataAnnotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?). Is there a way to tell RazorEngine what assemblies to use?- caused by DataAnnotations having a different namespace in 451/standard

thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
toddamscommented, Apr 12, 2017

Part with injector is not correct. Is was just an example of how I use PreRenderCallback to fill in dependencies in templates.

Here is more complete example:

var configuration = EngineConfiguration.Default;

configuration.PreRenderCallbacks.Add(templatePage =>
{
	var page = (YourBaseType)templatePage;

	page.Html = /* Assign your property here */,
	page.MyOtherProperty = "Same here";
});

var engine = EngineFactory.CreatePhysical("C:\\", configuration);

/* Use engine then */
1reaction
toddamscommented, Apr 10, 2017

Yep, inheritance is supported.

Create a class:

public class BasePage<T> : TemplatePage<T>
	{
		public override Task ExecuteAsync()
		{
			return Task.FromResult(0);
		}
	}

In your view:

@inherits YourApplicationNamespace.BasePage<dynamic>

Replace dynamic with your model type, or leave it as it is.

About assemblies: It takes dependencies from entryAssembly, so make sure that you have everything there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adjust Namespaces | ReSharper Documentation
Press Ctrl Shift 0R and then choose Adjust Namespaces. Right-click and choose Refactor | Adjust Namespaces from the context menu. Choose ...
Read more >
Organizing types in namespaces
Learn how namespaces help you organize related types.
Read more >
Change Default Namespace when creating Class in Folder ...
Right click on the project, go to properties and under the 'Application' tab there is a 'Default namespace' field. Note that this doesn't...
Read more >
Namespaces
To set the namespace for a current request, use the --namespace flag. For example: kubectl run nginx --image=nginx --namespace=<insert-namespace ...
Read more >
Namespace in C++ | Set 1 (Introduction)
A namespace is a declarative region that provides a scope to the identifiers (names of functions, variables or other user-defined data types) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found