I'm fairly new to both Umbraco and dotNet, so I wanted to see if I'm on the right track here or not. Let's say I want to have an Umbraco page that queries a key from Redis and displays its value. I don't necessarily want to set up a completely separate API route, I just want display the value within an existing page.
For a normal ASP dotNet Core WebAPI app, I would add the StackExchange.Redis package to the project, create the redis connection and add the database to the builder's Services in Program.cs, pick up the reference to the database via DI when the ApiController class is created, and then I could use that instance to run queries (as an await task) in any of that class's methods. Works like a charm.
Now I'm trying to figure out how this maps to Umbraco. I can create the redis connection and add the database to the builder's Services in Program.cs by extending IHostBuilder and tying into its init sequence. I can call my own C# method within a partial view's .cshtml code to display whatever string (or HTML snippet) I want within a given page.
But what I can't figure out is how in Umbraco I'm supposed to access classes registered in Services to calls within .cshtml code, especially given that eventually my queries will be dependent on parameters passed to the Umbraco page. Do I need to create a custom model and write my own SurfaceController to do a redis query or an HTTP call to another server?
A lot of the Umbraco documentation seems to presume a deep understanding of both dotNet and Umbraco's structure, and is mostly concerned with extending models stored in Umbraco's own database. The fact that tutorials on the web cover ten versions of Umbraco that are very different from one another doesn't help either. (I'm on 10.3)
Following up on my own question. The way I've approached this problem is to not attempt to do a database query within the partial view's code. Instead, the partial view puts together the HTML and JavaScript bits, then the JavaScript on the client makes AJAX calls to an endpoint backed by UmbracoApiController. The Redis IDatabase object can be registered in Program.cs, and then the UmbracoApiController can get the redis IDatabase object through dependency injection.
Query Redis from Umbraco
I'm fairly new to both Umbraco and dotNet, so I wanted to see if I'm on the right track here or not. Let's say I want to have an Umbraco page that queries a key from Redis and displays its value. I don't necessarily want to set up a completely separate API route, I just want display the value within an existing page.
For a normal ASP dotNet Core WebAPI app, I would add the StackExchange.Redis package to the project, create the redis connection and add the database to the builder's Services in Program.cs, pick up the reference to the database via DI when the ApiController class is created, and then I could use that instance to run queries (as an await task) in any of that class's methods. Works like a charm.
Now I'm trying to figure out how this maps to Umbraco. I can create the redis connection and add the database to the builder's Services in Program.cs by extending IHostBuilder and tying into its init sequence. I can call my own C# method within a partial view's .cshtml code to display whatever string (or HTML snippet) I want within a given page.
But what I can't figure out is how in Umbraco I'm supposed to access classes registered in Services to calls within .cshtml code, especially given that eventually my queries will be dependent on parameters passed to the Umbraco page. Do I need to create a custom model and write my own SurfaceController to do a redis query or an HTTP call to another server?
A lot of the Umbraco documentation seems to presume a deep understanding of both dotNet and Umbraco's structure, and is mostly concerned with extending models stored in Umbraco's own database. The fact that tutorials on the web cover ten versions of Umbraco that are very different from one another doesn't help either. (I'm on 10.3)
Thanks for any guidance anyone can give. Cheers!
Following up on my own question. The way I've approached this problem is to not attempt to do a database query within the partial view's code. Instead, the partial view puts together the HTML and JavaScript bits, then the JavaScript on the client makes AJAX calls to an endpoint backed by UmbracoApiController. The Redis IDatabase object can be registered in Program.cs, and then the UmbracoApiController can get the redis IDatabase object through dependency injection.
is working on a reply...