0

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.

balint
  • 3,391
  • 7
  • 41
  • 50
  • What you are trying to do is what you would normally do with WebForms. Is there a specific reason why you just aren't going in that direction? Your goal seems like it's defeating the entire point of using MVC. I may be incorrect, but just my initial thought. – cr1pto May 10 '16 at 21:00
  • @Richard_D: I read [this](http://stackoverflow.com/a/5125873/65189) comment: "We don't use MVC, just plain ASP.NET with razor pages being mapped with URL Rewrite module for IIS 7, no ASPX pages or ViewState or server-side event programming at all. It doesn't have the extra (unnecessary) layers MVC puts in code constructs for the regex challenged. Less is more for us." and liked the idea, and wanted to investigate :) – balint May 10 '16 at 21:39
  • Ahhh, I see. MVC is great for that reason - it doesn't force programmers to abstract away HTTP (webforms does this for you). I was more confused because you're using a code-behind (which is what webforms does by default). – cr1pto May 10 '16 at 21:56

1 Answers1

0

well, just use this in the codebehind:

HttpContext.Current.Request.QueryString["id"]

and everything works as expected.

balint
  • 3,391
  • 7
  • 41
  • 50