is there any api that i can use to find the .net type from document alias?
so why i want to do this
Actually, I am trying to override the default umbraco template controller.
In the index method, i have RenderModel (and document alias), I want to create an instance of .net strongly typed class (using reflection) .
I'm assuming you've already created the .Net classes? If so, this doesn't sound like an Umbraco-specific question. Here's how you can instantiate a C# class based on the name of the class as a string (i.e., the document type alias): https://stackoverflow.com/questions/13952109/get-class-by-string-value
I want to find out the strongly typed class for an umbraco document type. I am on Umbraco version 7.6 and are using Umbraco.ModelsBuilder in LiveAppData mode. The .net classess are automatically generated by Umbraco.ModelsBuilder.
so in controller,
public override ActionResult Index(RenderModel model)
{
var documentTypeAlias = model.Content.DocumentTypeAlias;
var stronglyType = GetStronglyTypedType(documentTypeAlias);
// here i will use reflection to create an instance
}
protected Type GetStronglyTypedType(string documentTypeAlias)
{
// here use documentTypeAlias to find out strongly typed class generated by Umbraco.ModelsBuilder
return typeof(GenericContentDocument);
}
That is, you can just specify the particular type in the action method:
public override ActionResult Index(GenericContentDocument model)
If you don't want to do that (e.g., if you have a bunch of types or if ModelsBuilder isn't storing them to the file system), you can use the reflection method I linked above.
Finding .net strongly types from ModelTypeAlias
Hello guys,
is there any api that i can use to find the .net type from document alias?
so why i want to do this
Actually, I am trying to override the default umbraco template controller. In the index method, i have RenderModel (and document alias), I want to create an instance of .net strongly typed class (using reflection) .
Any idea?
I'm assuming you've already created the .Net classes? If so, this doesn't sound like an Umbraco-specific question. Here's how you can instantiate a C# class based on the name of the class as a string (i.e., the document type alias): https://stackoverflow.com/questions/13952109/get-class-by-string-value
Thanks for replying @Nicholas.
Let me explain.
I want to find out the strongly typed class for an umbraco document type. I am on Umbraco version 7.6 and are using Umbraco.ModelsBuilder in LiveAppData mode. The .net classess are automatically generated by Umbraco.ModelsBuilder.
so in controller,
I see. Does
LiveAppData
mode actually store the classes to the file system, or is it purely in memory? If it does store them on the file system, you can do this: https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/78440-route-hijacking-using-models-builder-how#comment-250707That is, you can just specify the particular type in the action method:
If you don't want to do that (e.g., if you have a bunch of types or if ModelsBuilder isn't storing them to the file system), you can use the reflection method I linked above.
If you don't know the assembly used by ModelsBuilder, you can scan all assemblies for the types: https://stackoverflow.com/questions/4692340/find-types-in-all-assemblies
is working on a reply...