Graciously Hosted by MaximumASP        

Write ASP.NET without Web Forms or MVC !

Some time ago I tweeted [ Follow Me ] that you could write ASP.NET applications without using Web Forms or ASP.NET MVC

Frameworks are a convenience !  Web Forms, MVC, Cake, Symphony, etc.

So what if you want to use the power of the .NET framework, but want complete control over the client code and don’t want to be forced to use the MVC pattern.

No Problem – ASP.NET and Web Forms are not synonymous !!

Look at these 2 “Hello World” applications.

PHP Hello World

   1: <html xmlns="http://www.w3.org/1999/xhtml">
   2: <head>
   3:     <title></title>
   4: </head>
   5: <body>
   6:   <?php
   7:     echo ("This is a test !");
   8:   ?> 
   9: </body>
  10: </html>

ASP.NET Hello World.

   1: <%@ Page Language="C#" %>
   2: <html xmlns="http://www.w3.org/1999/xhtml">
   3: <head runat="server">
   4:     <title></title>
   5: </head>
   6: <body>
   7:     <% Response.Write("This is a test !"); %>
   8: </body>
   9: </html>

On the ASP.NET side, I could include YUI or jQuery and still use ASP.NET controls if I wanted or not use any server side controls and even turn off view-state.

If you don’t have call response.write you can even use the short hand …

<%= "This is a test !" %>

I can still code to the page lifecycle events if I want, or, code for top-to-bottom execution as seen above !

Form and Query String Variables are all accessible via the request object (and tons of other stuff)

Give it a try !

» Similar Posts

  1. Session Time Out Tricks
  2. Implementing a jQuery Modal Window in ASP.NET
  3. Comments on my recent benchmarks.

» Trackbacks & Pingbacks

  1. Pingback from Write ASP.NET without Web Forms or MVC ! | I love .NET!

  2. DotNetBurner - burning hot .net content

    Write ASP.NET without Web Forms or MVC ! — October 13, 2009 8:29 PM
  3. Pingback from Write ASP.NET without Web Forms or MVC ! : Misfit Geek

» Comments

  1. Osman avatar

    Thats what we call classic ASP:)

    Osman — October 13, 2009 1:14 PM
  2. Joe Stagner avatar

    To the contrary Osman. It's still .NET and that is the point.

    Joe Stagner — October 13, 2009 1:27 PM
  3. Bishop avatar

    Well actually you can do the same thing with code behind too :

    .aspx.cs file :

    protected string HelloMessage { get; set; }

    protected void Page_Load(object sender, EventArgs e)

    {

    HelloMessage = string.IsNullOrEmpty(Request["btnSubmit"]) ? "Hello World !" : "Hello Again World !!";

    }

    .aspx file :

    <body>

    <form action="testtemp.aspx" method="post">

    <div>

    <%= HelloMessage %>

    <input type="submit" name="btnSubmit" value="Say again ?" />

    </div>

    </form>

    </body>

    Now you have the best of the two world \o/

    I'm even sure there's a way to use the controls without having the ViewState (like calling their render method and writing that in the stream), but that's beside the point you were trying to make I guess ;)

    Bishop — October 13, 2009 1:52 PM
  4. Michael Washington avatar

    Ahhh I get your point.

    Michael Washington — October 13, 2009 2:11 PM
  5. John Goode avatar

    Ugh, why not just use the MVC pattern. Its cleaner, neater and you can actually test it. How would you test your Hello world example?

    John Goode — October 13, 2009 2:16 PM
  6. Joe Stagner avatar

    John, you're missing the point. The point is not to say "everone should do it this way", the point is that different develoeprs and different projects are well fitted by different approaches and ASP.NET offers the flexability to choose the one that best fits.

    Joe Stagner — October 13, 2009 2:20 PM
  7. Paul Taylor avatar

    Good article.

    When working on the .NET 1.1 Framework, I mandated that every developer working in the programming team completely eschew all ASP .NET controls ( and ViewState ) with four exceptions - UserControls, Repeater, PlaceHolder and in very rare circumstances - the Literal.

    There were a ton of good reasons for doing so. For starters, we were producing public facing websites. .NET 1.1 was not great at producing the exact markup we needed, even with a large browserCaps section.

    At the time, our designers were using CSS selectors based on the ID of an element. The client-side auto-naming was not helping us here. Plus, the use of .NET controls meant that

    We also wanted to keep page weight nice and low, which meant getting rid of viewstate. You might be amazed to know that we also dismissed all of the built-in Validators.

    Certain things were not great ( e.g. ternary operators on forms ) but overall, everybody benefited from the experience.

    Developers got to know a lot more about actual XHTML, most learned Javascript, the sites we developed after instituting the policy were significantly more performant and relationships between programming and design depts hit an all-time high.

    Even now, I'd still consider taking the same approach on a public site.

    The .NET Framework has been a revelation, and I couldn't be without it. However, part of the craft of using it is picking and choosing what works for you and your organization.

    Web pages essentially boil down to two things - GETs and POSTs. For all the layers of abstraction you might stack on top, that's what you're working with. I'm glad .NET was able to give me robust server side functionality without hampering my need for absolute control on the front end.

    Paul Taylor — October 13, 2009 4:05 PM
  8. Sean Patterson avatar

    Ahh, reminds me of my old days in PHP. 8^D

    As a sidenote, do you know if coding in this manner still generates the ViewState that you get with a typcial WebForms type model, or is this a clever way to avoid that for really simple pages?

    Sean Patterson — October 13, 2009 4:30 PM
  9. Joe Stagner avatar

    Sean, there is still "some" view state, but why not just turn all view state off if not useing server controls ??

    Joe Stagner — October 13, 2009 4:45 PM
  10. yaddamaster avatar

    One question - how do you define webforms? To me, the ASP.NET code you posted IS still webforms. Yeah, you've kinda eliminated the page-controller model since you don't have code-behind but you've still got controller code in-line with the aspx. If it's mapped to the System.Web.UI.PagehandlerFactory assembly (which it is) - you're still using webforms. If, on the other hand, you're implementing your own handler, or perhaps even putting a module in between and handling the rendering yourself - then you're not using webforms. Am I off-base here?

    yaddamaster — October 13, 2009 5:01 PM
  11. Joe Stagner avatar

    There is no use of Server Form processing or server conrols.

    One would be free to implement their application using pure HTTP / HTLM / JavaScript

    Joe Stagner — October 13, 2009 5:04 PM
  12. Paul Taylor avatar

    Yaddamaster - to me, a web form is an aspx page with a form tag with the runat="server" attribute set.

    I know this differs from what VS tells you when creating the page, but I'd agree with Joe on this point.

    Personally, I'd still use code-behind for all of my on-load functionality, reserving in-line to "tie-interceptor" string payloads :-

    <%= SomePropertyIDefinedEarlier %>

    Paul Taylor — October 13, 2009 5:18 PM
  13. dc avatar

    I think this is a good point to bring up because a lot of asp.net developers don't even think about this.

    I've always liked keeping things as simple as possible. As long as the code is easy to understand why use 10 files where 1 will do just fine. To me the concept of having an aspx, .cs, another .cs for business logic, another .cs for helpers, another .cs for data, another .cs for something else doesn't make sense when all you want to do is "hello world".

    The only thing I'd add about using <% %> on aspx pages... in 1.0, 1.1 didn't using those on the page mean that your page became interpreted and not compiled? It'd have to be compiled every load and that would affect performance so I never used it. At least this is what I thought then. I think in 2.0 that has changed and now everything is compiled... so now I have no problems using this other than having easier ways to do things.

    dc — October 13, 2009 5:46 PM
  14. zhangzhao avatar

    code-behind

    zhangzhao — October 13, 2009 11:24 PM
  15. zhangzhao avatar

    @Bishop:

    That's the way I prefer.

    zhangzhao — October 13, 2009 11:30 PM
  16. zhangzhao avatar

    I don't think Reponse.Write is a good method to output a string, right?

    zhangzhao — October 13, 2009 11:34 PM
  17. rwezowicz avatar

    Joe, AWESOME article ... as a switch hitter on code myself (PHP/VB.NET/C#), it's great to know that I could always switch back to this "basics" kind of programming scheme ... very cool.

    [as an aside everyone, it was Joe at an event in Nashua FIVE years ago who convinced me to try .NET (I only did PHP back then at PBS in Boston) ... now ASP.NET is my full time job]

    rwezowicz — October 14, 2009 12:12 AM
  18. rtpHarry avatar

    Well it was interesting to know how much you can strip asp.net back to the bones but that "Give it a try!" at the end is just going to cause arguments in these comments... oh wait they already started :P

    rtpHarry — October 14, 2009 4:28 AM
  19. .NET Freelancer avatar

    What benifits will getting such a control on the code give us?, when we have to start doing all the things from scratch.

    .NET Freelancer — October 14, 2009 5:54 AM
  20. Dave avatar

    If you're not going to use ASP.NET features then why not just use a *.htm file and use JavaScript, I don't really get this post :-)

    Dave — October 14, 2009 6:13 AM
  21. Joe Stagner avatar

    Dave,

    The point is that ASP.NET is WAY more than just the UI bits ofthe framework.

    You could use just .htm and JavaScript but you would still need to implement server side logic !

    Joe Stagner — October 14, 2009 6:38 AM
  22. Steffen Jaques avatar

    @Joe Stagner

    I see you've already been stormed by people saying "what? why this article?" .. I even stumbled over it and left wondering... "why did he write this?" -- I even sent it to a friend and asked him if he could see something that I was missing...

    Alas he couldn't find something, except, YES, it is infact possible to NOT use server-controls and code-behind in ASP.NET.

    This is probably something more novice developers can use for something, and aren't able to decypher themselves.

    But, for us more seasoned developers, could you please label these types of posts with something like "Beginner" -- I think more time has been wasted by seasoned developers, than has been gained by novice developers, by leaving out that VERY useful tidbit of information in the header ;)

    Am I wrong here?

    Steffen Jaques — October 14, 2009 7:02 AM
  23. anirdha Gupta  avatar

    Hello Misfit geek

    how to i became a successful web Developer

    anirdha Gupta — October 14, 2009 7:04 AM
  24. anirdha Gupta  avatar

    how to be bacame a successful web developer

    anirdha Gupta — October 14, 2009 7:32 AM
  25. webuser avatar

    What the man is trying to say is that you can still have the benefits of .NET, but you can control client side code exactly as you want, as i also prefer, Joe correct me if I'm wrong. And this is not some "beginner" stuff.

    webuser — October 14, 2009 8:47 AM
  26. Joe Stagner avatar

    webuser - you are exactly right, but you knopw, sometimes I just need to bite down and smile :)

    Joe Stagner — October 14, 2009 10:15 AM
  27. Craig avatar

    You know, this is really the sort of thing I think experienced developers need to remember. I cringed when I saw "Hello World" done as an MVC application. It's like bringing in a 20-ton rock crusher to drive a thumbtack into a cork board. Too many apps get over-coded and over-frameworked, IMHO. KISS is the rule.

    In my opinion, inline ASP.NET is a great alternative for simple pages that do not need to be an integrated part of a full application. Web forms (with Agile) is the best for a 1-2 developer small to mid-size application. MVC (with full RUP) is best for a huge application with a lot of developers. There's a time and a place for each method.

    Craig — October 14, 2009 10:28 AM
  28. Craig avatar

    @anirdha - I'm seeing you on lots of blogs today. Here is my advice:

    1) Read, read, read, then read some more

    2) Read to understand, not to simply turn pages

    3) Practice what you read

    4) Also read non-technical books to improve your understanding of the culture and language you are trying to communicate with.

    5) Learn to communicate better every day. I would not come to your native country and attempt to have conversations with you in your native language about advanced topics until I had improved my ability to use that language to the best it could be. This is crucial to effective communication, and removes the frustration that poor communication can cause. (In this case, keep learning to speak and write English better. It's my native language and even I have to remind myself of some rules from time to time.)

    These things will start you on the path to becoming a successful developer.

    Craig — October 14, 2009 10:43 AM
  29. Jef Claes avatar

    Hey Bill

    I did the top-to-bottom as well. I made a TwitterClient :) http://bit.ly/3wgSaO

    Jef Claes — October 17, 2009 12:12 PM
  30. SergeyS avatar

    > So what if you want to use the power

    > of the .NET framework, but want complete

    > control over the client code and don’t

    > want to be forced to use the MVC pattern.

    That is not "complete control over the client code" in my opinion. Still you are sending a form to a client. At max you can communicate data without resending the form.

    "Complete control over the client code" would mean that you can communicate logic, not only data asynchronously. That is, for instance, within the same form you could send a client a JavaScript code "On-Demand".

    Good idea, I'll try to blog on it in details with the code in a week or so.

    SergeyS — October 20, 2009 2:55 PM
  31. Matthew avatar

    I had only used ASP.NET for a couple months before I had similar sentiments. Web controls and MVC are both unnecessary complications. Give me Response.Write, Request.QueryString, Request.Form, and a good generic programming framework like C#/.NET; and I'll handle the rest, thank you.

    I felt so strongly that I put up an example page at

    agalltyr.com/.../rawaspdotnet.ht

    a little while ago. It's funny someone else was thinking the same thing.

    Matthew — October 22, 2009 5:58 PM
  32. DannyT avatar

    Anyone that doesn't "get this" imo is not a seasoned WEB developer. HTML markup and JS is web programming and being able to cleanly separate this skillset from asp.net is a worthwhile function. There is a LOT to be said for semantic html, progressive enhancement and creating efficient, usable and accessible web pages that often asp.net controls will get in the way of. Ask the rest of the web.

    That said, we do use web controls and I'm okay with that but that is a very specific skillset and will not be of any benefit in anything but a .net environment.

    DannyT — October 25, 2009 7:02 AM

Comments are closed