41

I'm liking the Razor syntax that Microsoft has developed for inline coding in their WebMatrix product (http://en.wikipedia.org/wiki/Microsoft_WebMatrix).

Now that Visual Studio SP1 has RTM'd, is it possible (and/or planned) to enable the use of Razor syntax in ASP.NET Webforms?

TylerH
  • 20,799
  • 66
  • 75
  • 101
cpuguru
  • 3,763
  • 5
  • 33
  • 31

5 Answers5

51

We (the ASP.NET team) currently have no plans to support the WebForms page model using Razor syntax. Furthermore it is unlikely that we would ever seriously consider this as the models are too different to make the two work together.

marcind
  • 52,944
  • 13
  • 125
  • 111
  • 1
    So what means this **You'll be able to use .cshtml/.vbhtml files within web forms based applications as well. Stay tuned.** http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx#7553469 ? – Programista Apr 29 '11 at 21:38
  • 1
    There are still no such plans. You can have a classic WebForms application and add Razor files to it but they can't have controls such as ``. – marcind Apr 30 '11 at 03:06
31

You can use Razor pages without MVC; this is called ASP.Net WebPages.

Just add .CSHTML files to a normal ASP.Net 4.0 project.

I explained how this works in my blog.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    Sure, you can create .CSHTML or .VBHTML files, but the question is whether Razor syntax can (or will be) usable in *.ASPX pages. – cpuguru Mar 10 '11 at 20:26
  • @cpuguru: Of course not. That wouldn't make any sense. You cannot (in general) use two competing languages in one file. – SLaks Mar 10 '11 at 20:31
  • 3
    I thought Razor was a syntax (not a language)? – cpuguru Mar 10 '11 at 20:57
  • 1
    Loved the explanation on your blog. Thanks for providing the link, it helped me understand Razor a tad better. – cpuguru Mar 10 '11 at 23:16
  • 3
    The difference is that that the lexical grammar of C# is not changed in any meaningful way. Only the syntactic flow... If the syntax was so different that you had to relearn the tokenization, it would not be the same language anymore. Just my $0.02, maybe someone else has a better perspective than this. – Joshua W Jan 17 '13 at 00:28
5

You could possibly integrate it using the RazorEngine available from Codeplex. It allows you to process razor outside of MVC. Though you don't get all the features you get from the MVC version of Razor, such as @Html and @Url and others.

Buildstarted
  • 26,529
  • 10
  • 84
  • 95
0

This really isn't that difficult to do. Working on it right now. Grab RazorEngine from CodePlex. It lets you compile Razor cshtml/vbhtml files into a class at runtime.

You can then take that class, and use it from a ASP.Net server control, inside its Render method. This is a great way to get HTML out of the body of a server control. Much, much cleaner.

Now, you can also add some methods that you can invoke from Razor. For instance, add something like RenderChild. Additionally, you could set the Server Control itself as the Model available to Razor. You could then invoke into the server control. Including doing something like grabbing one of it's child controls and invoking Render.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
Jerome Haltom
  • 1,670
  • 2
  • 17
  • 23
0

I dare say that Microsoft have considered it, but there is no official word on the subject. Since you are not supposed to have C# or VB code in the ASPX file, you have to wonder about the point of adding Razor support to Web Forms. The code behind would still be a (partial) class file. You wouldn't put Razor there any more than you would put it in a class file in Web Pages or MVC. And swapping Server Controls and all that good declarative stuff for Html Helpers removes one of the key reasons for going the Web Forms route, IMO.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • 1
    @SomeControl.ClientID instead of <%=SomeControl.ClientID%> seems like a good reason to want razor inside webforms. – Arshia001 Feb 02 '16 at 07:47