Click here to Skip to main content
15,607,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i make a project only with Razor pages?
My problem is that for example i want to create a connection with the database but in order to work with razor i have to open a new Database.Connection in every .cshtml file. How can i for example put the Database.Open(dbname); in a file like Layout.cshtml for example and call it in every file that i need it so instead of open a new connection in every file just instantiating a file with every code line that i might need like i would do with a c# file.

What I have tried:

My second problem is how can i do the first problem that i described above with c# files?
I have made a connection in a c# but whenever i instantiate the file DBConnection db = new DBConnection();
then how will i go about making a query in the razor view?
Keep in mind i am not looking for an MVC explanaition. Iwant a pure Razor Page and C# project.
Thank you.
Posted
Updated 24-Apr-19 8:01am

You cabn't do razor pages without controllers. The db work is done from the controller - not the view. The view simply presents the data to the user in the form of appropriate markup. Once data has been retrieved from the db, put it into a session var if you don't want to keep making the round trip to the db. Of course, I can't make specific recommendations because your question really is poorly stated.

EDIT ======================

You can try this:

GitHub - jlamfers/RazorMachine: RazorMachine is a robust Razor 2.0 templating engine that supports layouts as well as a _viewStart construct like Asp.Net MVC[^]
 
Share this answer
 
v2
Comments
simple world 24-Apr-19 11:30am    
Let me explain it better.
What i mean is that i want in my file explorer in the visual studio to have only razor and c# files.
I dont want to do anything with MVC or other frameworks.
All i want is to hard code every step of my project because i am still in the learning proccess of my career and need to know exactly what i am doing and why i am doing it.
That is the reason why i want to create a project where i will only have my razor views and the c# files to do the data proccess and logic.
But my main problem is how can i combine these two together.
For example how can a c# file that has a DB connection in it how can i use that in my razor view directly and run a query in the view without having to create a new db connection every time?
Thank you.
#realJSOP 24-Apr-19 12:03pm    
I updated my answer.
You can have a "using" in Razor; so you can include references to system and your own custom code libraries / dlls (e.g. sharing you connection code).

Here's a link.

Note the consternation at using query code in the cshtml; even though it works.

Make your own rules if it doesn't hurt anybody.

c# - How to use the "Using" statement in ASP.net razor webpages? - Stack Overflow[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900