views:

544

answers:

5

I want to build my own "code behind" like pages so that i can have HTML in a HTML file and code in cs file but be able to have Intellesense for the tokens in the HTML file. (i know that's what the .NET page class does, but i want to have something much lighter)

EG: in the .html file:

<%@ Directive classname="HTMLSnippet" %>
<html>
   <body>
     <div>[%message%]
   </body>
</html>

and in a .cs file

public class MyClass : HTMLSnippet
{
   public class MyClass () {
      snippet.message = "message goes here"
   }
}

So my question is how do make the HTMLSnippet class so that it's members are automatically created, and specifically show up in Intellesense as i add tokens to the .html file?

I know that .net currently does it by creating the designer.cs file and basically builds a class with all the elements from the page as it goes, and that would work fine but how can i get visual studio to generate that before compiling so that it shows up in Intellesense. Thanks!

Clarification I'm not using this as a handler yet, i want to use this to have HTML snippets with tokens be usable in code as an object with properties. so almost like a custom control. I think what i have to do is create a VS add-in that waits for me to type tokens into an .html file then it automatically creates a .cs file with members for each token.

+2  A: 

First, there is not a lot of bloat in an ASPX page, if you don't want the bloat. You can get rid of pretty much everything other than the @ Page directive tag. You can turn off viewstate, as well, if you do not want bloat on the client side, so I see no reason to reinvent the wheel.

If you want far less bloat, consider ASP.NET MVC. If I am guessing correctly, you will see the RTM at MIX, as Microsoft loves to release things at conferences. If not, it is RC2, so RTM is not that far away. In ASP.NET MVC, the ASPX pages can be extremely thin, as they are just views.

If you want to tackle this your own way, you can do what you want with HTTP Handlers to handle the type. I would not leave it as .htm or .html, as you will make it so you cannot serve a standard .html file on your server, which is not good. This solves the handling issue, which is only half your battle.

I am not sure the best way to handle Intellisense. One way is to reference, as shown on Stagner's blog: http://weblogs.asp.net/joestagner/archive/2008/05/12/add-custom-javascript-intellisense.aspx

I have not tried this with custom extensions, although it should work. You can also create your own custom XSD (XML) file, ala: http://vyasashutosh.blogspot.com/2007/05/providing-custom-intellisense-in-vsnet.html

Personally, I would use the ASPX model and thin out what you don't need. you can even whack the templates if you would like so you can do this with every page. You cannot get rid of the @ page directive, but if that is too much bloat, I am not sure ASP.NET is the model you wish to work with.

Gregory A Beamer
I love that people get defensive about the UI.Page class, heh, i'm VERY familiar with handlers, modules, and the ui.Page (controls...) I am looking to re invent the wheel but with improvements (eg rubber tread, steel bearings..) Thanks for the links!
rizzle
Creating custom XSD looks cool, but how do i get visual studio to build some XSD whilei'm typing in the IDE?
rizzle
+1  A: 

Not that i agree with you, but you can create protected variables on code behind and use them the way you are trying.

At code Behind:

protected string myVar1 = "Hello World"

At Html:

<html>
   <body>
     <div><%= myVar1 %>
   </body>
</html>

Again, I just want to make it clear that working this way is going one step back and I advise against it.

Sergio
this wouldn't work in a .html file, visual studio won't just parse html files nor will it expose those variables at the same level as the code behind class. But thanks, for offering an answer even though you disagree :)
rizzle
+1  A: 

Try MVC framework.

else use PHP, DJANGO, ROR or some other framework that supports templates fully.

TheVillageIdiot
A: 

I believe if you inherit your class from the abstract UI.TemplateControl, you will get this functionality. I'm browsing through the interfaces to see if simply implementing a specific interface will get this for you, but, I'm guessing if you want this functionality this is what you'll need to do.

overslacked
A: 

Or try using spark for asp.net it attempts to remove the need for inline script tags by using xml markup IE

<% foreach( string key in collectionOfItems.Keys ) %>
<div><% =key %></div>
<% } %>

becomes something like

<foreachcollectionOfItems.Keys >
<div>$key</div>
</foreach>

note my example is better crap and wrong but you get the idea. google it... spark

cdmdotnet