1

Code :

<html>
    <body> 
    @{
        if (IsPost) { 
            string companyname = Request["CompanyName"]; 
            string contactname = Request["ContactName"]; 
            <p>You entered: <br />
            Company Name: @companyname <br />
            Contact Name: @contactname </p>
        }
        else
        {
            <form method="post" action="">
            Company Name:<br />
            <input type="text" name="CompanyName" value="" /><br />
            Contact Name:<br />
            <input type="text" name="ContactName" value="" /><br /><br />
            <input type="submit" value="Submit" class="submit" />
            </form>
        }
    } 
    </body> 
</html>

I was referring to http://www.w3schools.com/aspnet/webpages_forms.asp to learn basic .NET and I came across this code. I made a new aspx file in visual studio and pasted the code. But its taking the Razor part '@' as text. Can anyone help me out with explaining what I should do to fix this?

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
Phoenix.1993
  • 85
  • 1
  • 2
  • 13
  • Did you paste it into a view (.cshtml) or in a html file? – Alex Suleap Feb 26 '16 at 08:51
  • I made a new aspx page in Visual studio named as Default.aspx and then pasted the code there. I also tried making a normal html file using notepad and found that both produced the same result. – Phoenix.1993 Feb 26 '16 at 08:52
  • not same but related question ask [here](http://stackoverflow.com/questions/5264852/can-we-use-razor-syntax-in-asp-net-webforms-aspx-pages) – ghodasara keval Mar 03 '16 at 11:45
  • not same but relative question http://stackoverflow.com/questions/5264852/can-we-use-razor-syntax-in-asp-net-webforms-aspx-pages – ghodasara keval Mar 03 '16 at 11:48

1 Answers1

0

Razor does not work in .aspx files. Plus you don't respect the ASP MVC principles:

You have to create first a controller. Inside the controller add an action. And for this Action generate a view.

This will be <ActionName>.cshtml file, which can be found in the folder Views/<ControllerName>.
In this file you can use razor.

The basic sample is : HomeController with action Index, you will find View in Views/Home/Index.cshtml

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Alex Suleap
  • 559
  • 4
  • 12
  • so are you saying that the famous website w3schools have made a bad code? ref - http://www.w3schools.com/aspnet/webpages_forms.asp – Phoenix.1993 Feb 26 '16 at 08:58
  • I do not now exactly what is there but please have a look at this [link](http://stackoverflow.com/questions/5264852/can-we-use-razor-syntax-in-asp-net-webforms-aspx-pages) – Alex Suleap Feb 26 '16 at 09:00
  • I have looked at the tutorial on [w3schools.com](w3schools.com/aspnet/webpages_forms.asp) and you didn't follow it correctly. You should have create and Web Page(Razor) and an Web Form. – Alex Suleap Feb 26 '16 at 09:21