ASPNetFAQ.com: What is ASP.NET?

Technology posts on ASP.NET, IIS, Windows (+ a little Linux), Cloud Servers, Hosting, and more!
  • Blog Home
Search the site...

Razor Hello There Sample!

Tweet
Share
0 Shares

This is my version of a Hello World application. I don’t want to just write “Hello World!” to the screen in a web page because, well, that’s just too basic. So what I’ll do is write a very simple form, post the form back to itself (the same page), collect data from the form, then display the data.

While extremely simple, this demonstrates a few different key functions of a web application – collecting and posting data, gathering that data, then doing something with the data. It also gives a simple demonstration of code logic mixed inline with HTML – a common practice when using Razor pages.

Like most of my code sample posts, I’m not specifically writing for efficiency, performance, or security here – just showing one (of many) way to accomplish something.

The below is the full text of a page I named HiThere.cshtml.

@{
    string userName = Request.Form["userName"];
}
<div>
    @if (!IsPost) {
    <form method="post">
        What's your name? <input name="userName" type="text" />
        <input type="submit" />
    </form>
    } else {
        <div>hi @userName!</div>
        }
</div>

Even though its simple, let’s break it down.

Lines 1-3 define a local variable of type string and assign it to whatever was posted to this page with a form input name of userName. If nothing was posted to the page then the variable of course will be null.

Line 5 checks to see if this is the first load of the page or if it was posted-to. If it was *not* posted-to (IsPost is a boolean check for a postback, ! means not) then I want the form to display and ask the user what their name is – and give them an opportunity to enter it and submit the form.

Line 10 is an extension of that check… if the form *was* posted-to, then don’t show the form and only show Hi <whatever>!.

That’s it! I said it was simple and it is! :)

Happy coding!

Brad on Google+

More from my site

  • Video: Introduction to ASP.NET C# DebuggingVideo: Introduction to ASP.NET C# Debugging
  • Manually Remove the W3 Total Cache WordPress pluginManually Remove the W3 Total Cache WordPress plugin
  • Log Parser: Pulling Valuable Data From IIS LogsLog Parser: Pulling Valuable Data From IIS Logs
  • Sending Email Code Sample (Razor, C#)Sending Email Code Sample (Razor, C#)
  • Video: Getting Started with Microsoft Azure for ASP.NET DevelopersVideo: Getting Started with Microsoft Azure for ASP.NET Developers
Tweet
Share
0 Shares

Comments are closed.

Proverbs 19:20

"Get all the advice and instruction you can, so you will be wise the rest of your life."

A Note On WordPress Hosting

Our main focus is of course .NET, but with a mix of Linux, virtualization, and other technologies. But if you're really looking for the best WordPress hosting specifically, read my WordPress host review to save yourself hassle AND money!




Recent Posts

  • What makes good web hosting?
  • jQuery Mobile C# ASP.NET and N5 Networks Software Repository
  • Open Source Bug Tracking Software and the Orchard Project
  • ASP.NET Development with Dreamweaver MX: Visual QuickPro Guide
  • Kendo UI Sample, ASP.NET Ajax Tutorial & More

Tags

ASP.NET Automation centos CMS css cytanium Development/Coding Email gmail Hosting htaccess http https IIS javascript Learning Linux logparser MySQL nginx openssl OrcsWeb performance PowerShell redirect RHEL security server SherWeb smtp SQL/Databases ssl System Administration telnet terminal tip Troubleshooting Ubuntu virtualization Visual Studio web farm web hosting Windows windows server Wordpress

Categories

  • ASP.net development
  • Development/Coding
  • Hosting
  • IIS (Internet Information Services)
  • SQL/Databases
  • System Administration
  • Virtualization
(c) ASPNETFAQ.com