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)
Related
I started a project using Asp.net core 2.1 and read several articles on the subject.
Asp.Net Core in the new architecture, as well as the previous, while using Razor pages, as in Asp.Net also allows you to write code to behind the page.
1- I think that the best place to inject dependency to the project is the controller classes. I didn't really understand what it would be like to write to code behind of the page in the new architecture. Also, if we look at optimizing usage, we should add re-dependency behind the page. What is logic
2- When I was dealing with the project, I realized that there is no relation between the Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext class and the Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext classes. If we want to upgrade any old Asp.Net project, is there any document related to architectural requirements with the reasons and best practices (as in the case of the DI mentioned above)
I don't have a problem with building the project, I'm just writing
here to document this topic in order to better understand it and not
to waste unnecessary effort.
please right click on Area > add new Scaffold item I guess if you are using VS 2017 pro+ you might get a list of pre-defined RazorPages you can add, I can say they are mostly related to Identity actions like login, register, change password, ... you can have them with minimum amount of effort on your application.
for my case if I am not going to implement a complex identity model or if I don't want to have something other than asp.net identity for my application membership the Razor Pages are the fastest way of doing that , generally, RazorPages are compared with Mvc ViewComponents (personally I don't like to use it frequently and application-wide) (i think razor pages are using MVVM pattern)
long story short, I believe that the razor pages are the fastest and the more organized way of doing some small features in a project or even doing small projects but imagine you have too many actions in a big project, then you will have tons of razor pages which will be so hard to maintain.
I can recommend that go for mvc approach and use razor pages as needed, for both cases you can still inject your class/interface either to controller constructors or Razor page constructors and still using built-in asp.net core DI (services.AddScoped, services.AddTransient, services.AddSingleton)
here is the source code for Razor pages, you can look into it if you are in the mood ;)
https://github.com/aspnet/Mvc/tree/master/src/Microsoft.AspNetCore.Mvc.RazorPages
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
is it possible to extend an existing ASP.Net MVC Website, written in C#, with IronPython. I want to make our website customizable. So my plan was, i can write additional controller and models in IronPython and views with "cshthml" and then load it dynamically in the website.
I've searched a lot and found some examples, but they always work with ASP.Net Web Forms.
Thank you!
These two pages found on Google help explain it in depth.
http://www.tikalk.com/net/write-aspnet-mvc-using-ironpython-resources
http://forums.asp.net/t/1527861.aspx?how+to+start+ironpython+in+asp+net
You will need to add a reference to the DLL in your references section of the project. Other than that, things should work as normal. I use IronPython in my business website without any issues whatsoever. If you get it running and find that there is a piece of code you are having trouble with, please don't hesitate to comment.
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.
How time consuming is it to move a website built using Webforms to one built using MVC?
I have an existing website built using asp.net webforms, but it is a little annoying. I want to use lots of javascript and ajax, but webforms makes this a little difficult. It tries to do too much for me, and thus makes it difficult to work with when I want to do things it doesn't expect.
As such, I've taken to only using the Page_Load event, and totally ignoring all the postback stuff - when I need to deal with submitting forms, I handle it manually from the Page_Load event with Request.Form["ElementName"], but most of the time I use javascript.
I've heard that asp.net MVC is much nicer to work with, so I'm thinking of porting my site over to this. Given that I am writing runat="server" only on standard HTML elements that I need to fill with data with .innerHTML =, how long will it take me to move over to MVC? Or is it a totally stupid idea to attempt this move?
well, as you've probably guessed, it's such a broad ranging question that it would be almost impossible to give advice on your individual scenario without seeing the scale of the existing codebase. However, there are many mitigating factors that you may want to think of regarding moving functionality across piecemeal, such as:
can I break down the existing functionality into discreet segments
does the exisiting site still require all current features
are there new features that I could introduce purely in mvc
do i need to extend the object model - EF code-first may give huge advantage via mvcscaffolding
the beauty with asp.net is that you can mix-match webforms and mvc together, thus you can chip away at functionality as required. Of course, there are so many resources on mvc on the web now, it'll be easy to get going quickly, so i'm sure you'll make good progress.
I think you'll really enjoy mvc - it's a no going back experience.
[edit] - there are a few interesting resources on the web that discuss the mix of webforms and mvc. however, the definitive one for me has to be scott hanselmans from a wee while back. still relevent in mvc3 as it was then:
http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx
also:
http://www.aspnetmvcninja.com/general/mixing-asp-net-mvc-and-webforms
Short answer, impossible to tell here.
Long answer, it depends...
It depends on how many sites you have to rewrite, complexity of the site and your architecture.
If all your logic is in the code behind files you will have a long walk.
If your architecture separates responsibility with use of services and abstraction, a lot easier.
If I should walk that walk, I would look into porting the code bit by bit. I've done this with JSF and Spring MVC, and it looks like this is doable with .Net too.
http://weblogs.asp.net/rajbk/archive/2010/05/11/running-asp-net-webforms-and-asp-net-mvc-side-by-side.aspx
Wanting to improve your code and your skills is never a stupid move!
It is possible to use them both within the same application. It isn't necessarily advisable, but if you set an MVC project up, you can tell it to ignore page requests that map to your webform pages.
You can then start moving over functionality as you need it.
If there is likely to be functionality you need to be available across MVC and webform pages, then this is going to get complicated and you are probably best moving to MVC in one go.
Personally, what ever you do, start small and straightforward and if you can bit by bit.
It would have to be a rewrite, not a port. MVC sites are built very differently from WebForms sites.
MVC is not based on the ASP.NET request lifecycle. There is no codebehind; business logic gets called from Controller classes; data is encapsulated in Models which are then passed to Views. It's a totally different beast from ASP.NET.
And be warned: once you've made the switch to MVC, you'll never go back to the old world of ASP.NET! :-)
Good luck!