I do have some text(template), which looks like "Hello #FirstName, Welcome to #Address". The original plan was to put this text file in a view-bag and pass it through razor and it will do the job.
Now, The problem that I am facing is that my team wants this transformation to be handled by a Service, So there is no controller and there is no view here BUT there is a need to use Razor view engine's capability to replace all # with server variables.
This sounded weird to me as well but is there any way that I can extract this capability of Razor view in a way that I can use this functionality in a restful service rather than sending this data to a .cshtml file?
This is possible indeed.
Rick Strahl blogged about hosting the Razor Engine in non-web applications.
And of course, this has been asked here before.
Yes, it is possbible. The necessary steps to do that is described here: Razor View Engine without MVC at all.
Related
With the latest release of ASP.NET CORE version 2.0 we now have Razor Pages which I really like because it shows a MVVM way of developing which I am already familiar because I've used it in UWP.
I was searching to develop web apps without js because I am a .net developer and I came across this framework known as DOTVVM , it is great because it gives some features of js frameworks like Angular, React etc. and it also integrates very well with ASP.NET CORE 2.0
I want to discuss this between following 3 scenarios :
I should use DOTVVM and not use JS at all.
I should use Razor pages instead with asp.net core 2.0, because razor pages provide all functionality which dotvvm provides and it will have more community in future.
I should learn a JS framework like Angular or React because neither razor pages, nor dotvvm is powerful enough to replace them.
The discussion can be from point of view of all developers, so I can get opinion of JS developers as well as those who have worked with dotvvm or Razor pages. The aim is to create completely interactive and responsive web app with good performance.
**Important : ** the web app created can be a progressive web app from the start or maybe turned into a pwa after creating it, depends on the technology I choose at the end.
I know for a progressive web app I will need to write a little JS, but that is alright considering that is very simple and little to write.
The reason of this question is so that we can discuss these options from multiple perspectives and deduce a better option at the end for my app.
It really depends on the type of the application you are trying to create. The fact is that you can build any application in any web technology. The difference is only in how difficult it would be.
Razor Pages are very easy to use and they are part of ASP.NET Core platform. They are using the MVVM pattern, but they are not helping with client interactivity. If you need to hide some form field based on some condition, you would need to do it in JavaScript.
I would use them for a simple company website, very simple admin sites with few pages and trivial forms, or for a public facing part of some eCommerce site. I would probably use also jQuery or other libraries to do the interactivity, animations and things like that.
DotVVM is an MVVM framework which helps especially with web applications with complicated UI: lots of complex forms with dozens of fields and interactivity (when you select an item in a combobox, another combobox will get new values, some fields will be pre-filled, a part of the form will disappear or change and so on). Modal dialogs, multi-step wizards, lots of grids with data, sorting, paging...
Because DotVVM handles many things for you and translates the data-bindings to JavaScript, you don't need to write the JS yourself. It doesn't mean that you should not learn it. It will help you a lot if you know it - you will be able to add even more interactivity, extend the framework when you need it and more. DotVVM lets you do quite a lot without knowing JS, but you will get much further if you know it.
Angular, React or other client-side frameworks are used widely today and many websites and applications are built in that. They are more universal than DotVVM, which aims primarily at the business app segment and requires .NET on the server (because it is not only the client-side library).
Using Angular or React requires the knowledge of JavaScript, and not only that. You will need to know Node.js and the related tooling, like npm. You will need to exchange data with the server, so you will need to build probably a REST API, use Swagger to generate the client-side proxy classes, and then you will probably need some other JS libraries with controls and things like that.
It is quite a lot of things to learn and if you are used to the comfort of Visual Studio, like the IntelliSense or the debugger, you will miss these features for sure.
And finally, you don't need to stick to one technology. You can combine Razor Pages with DotVVM for example. You can make the admin dashboard in DotVVM and the public part of the website in Razor Pages.
Microsoft is coming back to the WebForms approach to simplify the project structure trusting in the "Convention over configuration" mantra, while hiding the configuration from developer to make things faster. But it has the disavantage that everything will be mixed again. I doesn't look like a smart move for organizing. But... Hey! Something new must catch the attention of the dev towards Microsoft.
If your page uses an MVC Web API for the REStful, it's really more easy to just use Razor pages. If not, I would recommend you to use Core MVC.
What kind of framework is Razor? Is it backend or frontend?
What is the difference between the two types of frameworks?
I'm trying to learn a little bit more about backend and frontend frameworks and since I usually work with Visual Studio Asp.net MVC was wondering about it.
It is not a framework . I think you're misinterpreting certain concepts. Razor is a server side view engine, and it uses C # or VB.NET to generate dynamic content.
Razor Syntax Quick Reference
This question is a couple of years old, but I'm going to add my two cents.
People struggle to give an answer to this question because the terms 'front-end' and 'back-end' aren't formally defined anywhere. Because of that, any answer is purely subjective.
That being said, it is my opinion that the relationship between front-end/back-end and client-side/server-side isn't necessarily one-to-one
I think it helps to think of it like this: client-side and server-side are run-times, while front-end and back-end are a separation of concerns.
'Client-side' always refers to code executing on the client's machine and 'server-side' always refers to code executing on the server. A 'front-end' developer deals with displaying data to the user and getting data from the user, while a back-end developer deals with storing, manipulating, and retrieving that data.
Consider a front-end developer who is tasked with building a UI. Much of the code they write will be the typical HTML / CSS / JS. However, they will also have to deal with the data that is passed to the front-end from the backend. This is where Razor comes into play. The front-end developer will write the Razor code (which executes on the server-side) to display the data.
That is, the front-end developer will write server-side code to help generate the UI, in addition to writing the client-side code that really defines the UI.
Now, I can't imagine a scenario where a back-end developer will write client-side code.
So, to answer your question, Razor is a front-end technology that executes on the server-side runtime. It's only purpose is to generate the UI, which is the concern of the front-end.
Razor is for writing dynamic html page which is front end and c# is for writing backend logic. Although you could move all the backend logic inside razor but its highly not recommended.
Razor allows you on the back-end more easily create views (.cshtml in C#).
It is more like templating system...
http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c
I have to use Sitecore 6.5 with MVC framework and as per the reference document I can setup Sitecore and make that running. I have created MVC3 .NET web application project (blank and using Razor) and I dot know following things
How do I setup Layouts and Sublayouts? (this must be different then web form I guess)
What do I have to do to use Sitecore data controls e.g. FieldRenderer, Text etc. (I guess without those controls it will be not easy for us to use provide Page Editing mode)
How do I use Sitecore placeholders in MVC?
I have used this document as a reference.
At present Sitecore MVC support is setup so that ASP.NET MVC can work alongside a Sitecore installation under the same hostname.
Full support for using MVC with Sitecore will be coming in the 6.5.1 update around June time. At that point all of the things you mention plus other stuff like populating the viewbag with context data will be possible.
There are ways to achive certain parts of this integration such as a shared source module that allows Razor usage. I have personally written a small module that lets you use MVC routes with sitecore items (Sitecore MVC Routing) but for full MVC support you will have to wait until June.
Someone has made a proof of concept of the things that you want.
This blog post explains it all:
http://www.chrisvandesteeg.nl/2012/02/26/sitecore-mvc/
None of this functionality is currently easily available with MVC. Sitecore allows you to use them side-by-side, and to make some calls to the Sitecore data layer, etc., but the level of integration you (and I) are looking for has not been released yet. Supposedly coming out soon though.
I wanted to give a try to razor but there are not many tutorials on web; I've already tried this one, so better ask here. Can anyone please tell me how razor works? I mean are there any code behind pages or controls? and what is done in code behind pages if they exist? Also, how can I reference code behind pages? Generally speaking, what type of code fits in the cshtml page, I mean, for the sake of a clear design how should be the structure etc etc. finally, any good tutorial is highly appreciated. Thanks!
Please note: I'm not using visual studio, I'm using webmatrix.
I can't speak a whole lot to WebMatrix. But...are you familiar with ASP.NET MVC? Given what you're asking, it sounds like maybe you aren't. I would start with learning the basics of MVC first. I learned on the NerdDinner example
http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx
To answer your question more directly, no there are no code behind files (though a code behind is technically possibly through some hackery). The paradigm is completely different from web forms. Essentially you have classes called controllers that accumulate the data for a page. The "data" for the page is referred to as a model and closely resembles what your problem domain is. Models are classes intended to represent objects in the system (customers, users, orders, widgets, etc...). The controller passes models to the view. The view renders the page. So the code you'd expect to find in each of these is:
Model: object representing the problem domain like Blog, Person, User, etc...
Controller: Gets data from database using services that return Model objects (or calls into entity framework, NHibernate, whatever)
View: displays that using cshtml, aspx or whatever the ViewRenderer is
So, in your view (cshtml), you're really just going to have straightforward programming for displaying UI elements and performing UI logic. You'll have mostly HTML and then some bits scattered around to pull data from the model and display it like
<td>#Model.Name</td>
<td>#Model.Description</td>
Hope that helps
EDIT: After reading a bit more, sounds like WebMatrix is kind of a RAD tool for web apps. Looking at the tutorial that you posted, looks like they're showing you go treat it like classic ASP and put everything right on the cshtml page. This is an option. I wouldn't recommend it though. It'll make life tough, but I'm unsure if there are better ways in WebMatrix.
Razor is a view engine, which can be used by itself in Razor pages (.CSHTML/.VBHTML) or in ASP.NET MVC. If you use it by itself, it's kind of like writing classic ASP or PHP, where the code is all inline. No code-behinds. Here's a brief introduction to the syntax:
http://www.mikesdotnetting.com/Article/153/Inline-Razor-Syntax-Overview
That site also has a lot of other great articles about the nature of Razor.
create App_Code folder put cs file to this folder
convert App_Code to dll in bin folder then upload bin folder
(by visual studio 2012/publish web site)
I want to write a C# program which can generate a series of HTML pages based on some data grabbed from an external database. Are there any .NET based SDK which enables me to do the HTML generation? Maybe I am searching on the wrong keywords, but can't seem to find a simplistic, comprehensive answer to this. Just wondering as well, is the Google Web Toolkit an answer to my problem IF I use Java instead? Thanks for your help.
In general, I would avoid server-side HTML generation, and opt for client-side templates.
There are many options when it comes to client-side templating, but jquery-tmpl is becoming a the standard template engine for jQuery.
If you want this to tie into your data easily, consider using MVC3 with Knockout.js, as demonstrated here: http://channel9.msdn.com/Events/MIX/MIX11/FRM08