I could figure this out at the end. Thank you for your answers!
1. Create a class for our virtual page
First thing is to create a class i.e ProductVirtualPage
which inherits from PublishedContentWrapped
. This last class provides an abstract base class for IPubslihedContent
implementations that wrap and extender another IPublishedContent
. We will use the property Product to get the Product
from the Razor view and render.
public class ProductVirtualPage : PublishedContentWrapped
{
private readonly Product _product;
public ProductVirtualPage(IPublishedContent content, Product product ) : base(content)
{
if (product.Name == null) throw new ArgumentNullException("productName");
_product = product;
}
public Product Product
{
get
{
return _product;
}
}
}
2. Create our handler based on UmbracoVirtualNodeRouteHandler
In the tree we need to create a reference node, which in my case it has the id 3286. We will pass it to the ProductVirtualPage(nodeReference, product)
method when the FindContent()
method is called.
public class ProductVirtualNodeRouteHandler : UmbracoVirtualNodeRouteHandler
{
private ProductService _productService;
protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
{
// Get the real Umbraco IPublishedContent instance as a reference point
var nodeReference = new UmbracoHelper(umbracoContext).TypedContent(3286);
//Get the product Id from the URL (http://example.com/Products/product/57)
var productId = umbracoContext.HttpContext.Request.Url.Segments[3];
// Create an instance for the ProductService
_productService = new ProductService(new ProductRepository(), new SiteRepository());
// Get the product from the database
var product = _productService.GetById(Convert.ToInt32(productId));
// Get the virtual product
return new ProductVirtualPage(nodeReference, product);
}
}
3. Register our custom route
We need to add our custom route using MapUmbracoRoute
and bind it to the ApplicationStarted
method that provides ApplicationEventHandler
.
public class ContentServiceEventsController : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
// Add a route for the Products
RouteTable.Routes.MapUmbracoRoute(
"ProductPage",
"Products/Product/{id}/",
new
{
controller = "Product",
action = "GetProduct"
},
new ProductVirtualNodeRouteHandler());
}
}
4. Create the controller
The controller will just return the View, passing our RenderModel model
.
public class ProductController : RenderMvcController
{
public ActionResult GetProduct(RenderModel model, int id)
{
return View("Product", model);
}
}
5. The view
@{
Layout = "~/Views/Shared/_MasterLayout.cshtml";
var productModel = Model.Content.Product;
var product = productModel as Product;
}
<pre>
Product Info
---------------
Id: @product.Id
Name: @product.Name
</pre>