I'm trying to implement a small templating app, which would generate simple html outputs from an xml data source.
I've created a new Razor website, have a custom razor view and a code behind class for it as well:
Employee.cshtml:
@inherits Employee
@{
Layout = "~/_SiteLayout.cshtml";
}
<div> @this.Name /*works OK from codebehind */ </div>
<div> @(Request.QueryString["id"] ?? "") /* works OK here */</div>
Employee.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Xml.Linq;
public abstract class Employee : System.Web.WebPages.WebPage
{
public Employee()
{
// load xml, etc.
string id = Request.QueryString["id"] ?? "default"; // Fails: Request is null
}
public GetValue(string id) { ... } // works OK
public string Name { get { return GetValue("name"); } } // works OK
}
If I call http://localhost:xxx/Employee.cshtml content is generating OK, if I try the default routing http://localhost:xxx/Employee it is working as well.
Is it possible to get the Request object in the abstract class? I would not like to go the true MVC way, it would overcomplicate this.
Update: I understand, that I can add a call to the cshtml template like:
@{
this.LoadEmployeeById(Request.QueryString);
}
and handle it from the class, just the "who and how/when executes my template with the working context" part is not clear for me.