Getting Started - The URL Routing

libron

New member
Joined
Nov 2, 2012
Messages
3
Programming Experience
3-5
I have been programming with PHP for some time before. And as of yet I am looking to get started with VB.NET by looking into the URL routing (or also the "http" protocol system) of it.

The intention is to build a "HelloWorld" web application, where the message should be printed on the screen by pressing the button.

Here is an example with PHP to showcase it:

HTML:
<body>
    <form action="" method="POST">
        <button name="button">Press It!</button>
    </form>


    <?php
    $button = $_POST['button'];
    
    if(isset($button)) {
        echo "Hello World Message!";
        }
    ?>
</body>

A showcase of this PHP example can be seen here: http://zikanti.com/myroot/helloworldmessage.php


This is how I imagined it with VB.NET:

FrontPage.vbhtml

VB.NET:
<h1>Cause the HelloWorld Line...</h1>

<form name="hello_world_button" action="[COLOR=#ff0000]@Url.Action("HelloWorld", "~/~/Models/HelloWorldController.vb")[/COLOR]" method="post">
    <button type="button" name="button">Press it!</button>
</form>


HelloWorldController.vb

VB.NET:
Module HelloWorld
    ' Every console application starts with Main
    Sub Main()
        System.Console.WriteLine("Hello world!")

    End Sub
End Module



The Folder Directory

VB.NET:
Models/HelloWorldController.vb
Views/Home/FrontPage.vbhtml



The aim was to have the module "HelloWorld" called up after the button is pressed, so the "HelloWorld" message does get printed on the screen.

This is where the URL routing does take place.

I tried the command "Url.action" as it can be seen above, though when testing the web application on the localhost server the message would not get printed after pressing the button.

Though, by this I am also looking now for a way to be able to tell if the URL routing has been done correctly. Is there a way for this?


Basically, to sum it up - I am not sure:

- if this example has been approached right and perhaps a different approach may be recommended
- if there is a way to make sure the URL routing is correct, before investigating why the "HelloWorld" message does not get printed on the screen.
 
Last edited:
That's all messed up I'm afraid. A route is supposed to invoke an action. An action is a public function of a controller. A controller is a class that inherits the Controler class. As the name suggests, controllers reside in the Controllers folder and models reside in the Models folder. A model is a type that represents data. A controller works with models to to move them from a data source to a view and back again.

Apart from all that, you are trying to display "Hello World" to the user by calling Console.WriteLine. That code is going to be executed on the server so, if the text was displayed anywhere, it would be displayed on the server, not the client. You need to either write to the response directly or else display a view that contains the text to display. I'm guessing that Response.Write is the closest equivalent to the 'echo' command from PHP but that rather defeats the purpose of creating an MVC app. You should create a view with the "Hello World" text on it and then have your action return that view. That's how MVC generally works: invoke an action in a controller and display a view.

Also, you have a form that is supposed to be posted but no 'submit' button. You have a regular button but that's just going to raise its 'onclick' event. Unless you handle that event and explicitly post the form it will not get submitted.

So:

1. Change your 'button' element to an 'input' of type 'submit'.
2. Add an actual controller to your project and add an action to that, i.e. a public method that returns an ActionResult.
3. Add a view for that action that contains the text you want to display and then return that view from the action.
4. Change your Url.Action call and pass as argument just the name of the controller and the name of the action. For instance, if you have a controller named HelloWorldController and an action named DisplayGreeting then the arguments will be "HelloWorld" and "DisplayGreeting".
 
Back
Top