使用 MVC 的约定方法为我的操作提供静态内容
我正在考虑在我正在创建的大型 Web 应用程序上外包一些页面内帮助,并希望在我们准备好时能够轻松地将这些内容添加到我们的页面中。
所以我想我可以创建一个系统,在其中我可以将文本文件添加到操作期望其视图所在的同一文件夹中,并读出该文件的内容,该文件中的内容可以传递回视图以显示。要么创建一个可以做同样事情的助手。
示例
Controllers
HomeController.cs
Views
Home
Index.aspx
Index.txt
然后,Index.aspx 将可以访问 Index.txt 中的内容。
我将如何开始创建这个系统。 .NET MVC 中是否有我可以利用的内置类?
I'm looking at outsourcing some in-page help on a large web application I am creating and would like to make it really easy for this content to added to our pages when we're ready for it.
So I was thinking I could create a system where I can add text files to the same folder where an action expects it's view to be and read out the content of that file the content in that file can be passed back to the view to display. Either that or create a helper that would do the same.
Example
Controllers
HomeController.cs
Views
Home
Index.aspx
Index.txt
Index.aspx would then have access to the content in Index.txt.
How would I start going about creating this system. Are there any built in classes in .NET MVC that I could take advantage of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
昨天提出了类似的问题: 包括静态 html文件从 ~/Content 到 ASP.NET MVC 视图。
基本上,您可以使用
File.ReadAllText
从文件中读取文本并将其包含在视图中,这样您的 index.aspx 文件中就会有类似的内容A similar question was asked yesterday: Including static html file from ~/Content into ASP.NET MVC view.
Basically you can read the text from the file and include it inside your view by using
File.ReadAllText
so you would have something like this inside your index.aspx file我会在 Content 文件夹中创建一个并行层次结构,并将文件放在那里,可能是 HTML 形式。然后,您可以使用并行层次结构约定通过 AJAX 在视图中简单地加载它们。
然后在您的视图中,
如果您的路线一致,您还可以从视图中的 RouteData 提取控制器/操作,并将其移动到您的 _Layout.cshtml 文件,并使用路线数据提供的路径。
I'd create a parallel hierarchy in the Content folder and put the files there, probably as HTML. Then you can simply load them via AJAX in the view using the parallel hierarchy convention.
Then in your views
You may also be able to extract the controller/action from RouteData in the view if your routes are consistent and move this to your _Layout.cshtml file with the path being provided by route data.
一种可能的解决方案是将它们存储为 xml 文件,这些文件是从视图期望的模型序列化的。然后,您可以创建一个操作筛选器,用数据填充返回的模型来自 XML 文件。我希望这有帮助。
One possible solution would be to store them as xml file instead, that are serialized from the model the view is expecting. You could then create an Action Filter populate the model being returned with the data from the XML file. I hope that helps.