Can we use Razor syntax in ASP.NET Webforms (*.asp

2019-01-05 04:06发布

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?

Thanks!

5条回答
仙女界的扛把子
2楼-- · 2019-01-05 04:11

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.

查看更多
做个烂人
3楼-- · 2019-01-05 04:13

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.

查看更多
Emotional °昔
4楼-- · 2019-01-05 04:24

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.

查看更多
等我变得足够好
5楼-- · 2019-01-05 04:34

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.

查看更多
▲ chillily
6楼-- · 2019-01-05 04:34

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.

查看更多
登录 后发表回答