项目纯粹是剃刀网页 [英] Project made purely of razor web pages

查看:70
本文介绍了项目纯粹是剃刀网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能使用Razor页面制作项目?

我的问题是,例如我想创建与数据库的连接但是为了使用剃须刀我必须打开一个新的每个.cshtml文件中的Database.Connection。我怎样才能把Database.Open(dbname);例如在Layout.cshtml这样的文件中,并在我需要它的每个文件中调用它,而不是在每个文件中打开一个新连接,只是用我可能需要的每个代码行实例化一个文件,就像我会用ac#文件做的那样。



我尝试了什么:



我的第二个问题是怎样才能我用c#文件做了上面描述的第一个问题?

我在ac#中建立了连接但每当我实例化文件时DBConnection db = new DBConnection();

那么我将如何在剃须刀视图中进行查询?

请记住,我不是在寻找MVC解释。我想要一个纯粹的Razor Page和C#项目。

谢谢。

解决方案

没有控制器你就不能做razor页面。数据库工作是从控制器完成的 - 而不是视图。视图仅以适当的标记形式向用户显示数据。从数据库中检索数据后,如果您不想继续往返数据库,请将其放入会话变量中。当然,我无法提出具体的建议,因为你的问题确实陈述得不好。



编辑=============== =======



你可以试试这个:



GitHub - jlamfers / RazorMachine:RazorMachine是一个强大的Razor 2.0模板引擎,支持布局以及像Asp.Net MVC一样的_viewStart构造 [


这是一个链接。



请注意在cshtml中使用查询代码时的惊愕;即使它有效。



如果不伤害任何人,请制定自己的规则。



c# - 如何在ASP.net中使用Using语句剃刀网页? - 堆栈溢出 [ ^ ]


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.

解决方案

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[^]


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[^]


这篇关于项目纯粹是剃刀网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆