外包@functions(Outsource @functions)

编程入门 行业动态 更新时间:2024-10-26 12:32:03
外包@functions(Outsource @functions)

我使用ASP.NET和Razor作为我的视图引擎。

不同的.cshtml文件 ,但每个 文件 都包含以下代码:

@functions { // Here some of my code, but it's the same code for each .cshtml file... }

我的问题是:我如何将这个@functions外包给外部文件? 因为目前我在所有文件中都有相同的代码,我不喜欢这样。

I'm using ASP.NET with Razor as my view-engine.

There are different .cshtml files, but each of them contains the following code:

@functions { // Here some of my code, but it's the same code for each .cshtml file... }

My question is: How can I outsource this @functions to an external file? Because at the moment I have the same code in all the files and I don't like that.

最满意答案

“部分视图”是我一直在寻找的术语。

非常感谢Pranav Singh的有益评论。

教程:

部分视图允许我们将HTML和C#代码放入一个文件中,我们可以在多个其他视图中重用这些文件。 当您处理大型应用程序时,这非常有用。

假设我们有以下代码,我们希望其他视图可以重用。

在此处输入图像描述

我们将提取上面标记的代码并将其粘贴到一个新的“局部视图”中,然后我们将从这个foreach循环中调用“局部视图”。 请使用以下内容。

步骤1

选择上面的代码并将其复制到剪贴板。

第2步

右键单击任何视图文件夹以创建新的“局部视图”。

在此处输入图像描述

您可以为此视图页面使用任何名称,但建议在表示可重用的视图名称之前使用下划线符号(_)。 您还可以在上面的对话框中选择“模型类”来创建强类型视图。 请记得选中“创建为部分视图”复选框。

第3步

现在,在“局部视图”页面中粘贴您上面复制的所有代码。 您可能需要修复一些错误,例如@ item.Name将成为@ Model.Name。 这是完整的代码:

在此处输入图像描述

步骤4

还有一件事,你需要在我们复制代码的位置放置一个引用,这里你去:

在此处输入图像描述

请记住传递“部分视图”名称和型号名称。 现在,运行应用程序,您的应用程序将工作相同,因为我们只是在应用程序中设置了一个新结构。

希望这可以帮助。

所有学分都归Abhimanyu K Vatsa所有 ,本教程在这里找到。

"Partial View" was the term I was looking for.

Thanks a lot to Pranav Singh for his helpfull comment.

Tutorial:

Partial View allows us to put HTML and C# code into a file that we can reuse across multiple other views. This is very useful when you work on a large application.

Assume we have the following peice of code that we would like to make reusable for other views.

enter image description here

We will extract the above marked code and paste it in a new "partial view" and then we'll just call that "partial view" from this foreach loop. Please use the following.

Step 1

Select the above code and copy it to the clipboard.

Step 2

Right-click on any view folder to create a new "partial view".

enter image description here

You can use any name for this view page but the underscore sign (_) is recommended to be used before the view name that represents something that is reusable. You also have the opportunity to select the "Model class" in the above dialog to create a strongly typed view. And remember to check the checkbox "Create as a partial view".

Step 3

Now, in the "partial view" page paste all the code you copied above. You may need to fix some errors here like @item.Name will become @Model.Name. Here is the complete code:

enter image description here

Step 4

One more thing, you need to place a reference at the location from where we copied the code, here you go:

enter image description here

Remember to pass the "partial view" name and model name. Now, run the application, your application will work the same because we just set up a new structure in the application.

Hope this helps.

All credits goes to Abhimanyu K Vatsa, this tutorial was found HERE.

更多推荐

本文发布于:2023-08-02 12:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:外包   functions   Outsource

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!