Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Razor without MVC [duplicate]

I need to merge templates with data to create unique strings at runtime. It was suggested that I look at the Razor templating view engine that comes in ASP.NET MVC 3. Looks great, but I need to use it outside of MVC view creation.

I know I can use the core Razor engine directly, but I've also found a couple of projects that make using Razor directly easier. Like:

  1. Rick Strahl's Razor Hosting Template Engine- http://www.west-wind.com/weblog/posts/864461.aspx
  2. Razor Engine on GitHub - https://github.com/Antaris/RazorEngine

Does anyone have any guidance on using Razor outside of MVC as a standalone template engine? Any experience with these wrapper projects? are there other Razor hosting implementations I should look into?

like image 223
Michael Levy Avatar asked Jan 26 '11 18:01

Michael Levy


People also ask

Does Razor use MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.

Is Razor pages better than MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

Can you mix Razor pages and MVC?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.

Should I learn Razor pages or MVC?

MVC is good for those web application which has lots of dynamic server pages, REST APIs and AJAX call. For basic web pages and simple user input forms, we should go for Razor pages. Razor pages are similar to ASP.Net web forms in the context of structure, except page extension.


2 Answers

You may take a look at the following blog post which illustrates how you could use the Razor view engine to render a template to a string.

like image 50
Darin Dimitrov Avatar answered Sep 28 '22 02:09

Darin Dimitrov


Look at RazorTemplates library. It's more lightweight than RazorEngine library, it's thread-safe and has very nice minimal interface.

like image 35
alexey Avatar answered Sep 28 '22 00:09

alexey