将视图渲染为字符串以进行站点搜索 [英] Rendering a view to a string for site search

查看:82
本文介绍了将视图渲染为字符串以进行站点搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据驱动的网站。



要实现搜索功能,我会遍历记录(太少而无法编制索引),寻找重要字段中的搜索文本,分配分数,以及可以发送到视图进行渲染的搜索结果。



有几页是不是数据驱动的。它们本质上是静态文本(隐私政策,关于我们等),它们生活在他们自己的Razor视图中。因此,目前没有搜索到它们。



我曾希望我可以实例化一个控制器,告诉它将这些视图呈现为字符串,搜索它们并且我会我在途中。情况并非如此。经过几个小时的尝试,我无法完成这项工作。主要的问题是,如果没有 ControllerContext ,你就无法做到这一点,你不能只创建一个。



我实际上可以从文件系统中读取视图,但这很icky并且还包含代码和元数据。



这是可能的,还是我需要重新思考我呈现静态内容的方式?

I have a data-driven website.

To implement a search feature, I iterate through the record (there are too few to bother indexing), look for the search text in important fields, assign a score, and voila -- search results that can be sent to a view to render.

There are a few pages that are not data driven. They are essentially static text (privacy policy, about us, etc.) that live in their own Razor views. These are therefore not searched currently.

I had hoped that I could just instantiate a controller, tell it to render these views as strings, search them and I'd be on my way. That isn't the case. After a few hours of trying, I haven't been able to make this work. The main problem is that you can't do this without a ControllerContext, and you can't just create one.

I could actually read the views from the filesystem, but that's icky and would also contain code and metadata.

Is this possible, or do I need to rethink the way I'm presenting static content?

推荐答案

我有类似的问题,并将静态内容移动到具有视图/媒体的表/库保持。从而为您提供2个优势。

#1。可维护性,因为它没有直接硬编码。

#2。可搜索,因为它现在是数据驱动的。
I have a similar issue, and by moving the static content to a Table/Library which has a view/medium to maintain. Thus giving you 2 advantages.
#1. maintainability, as its not directly hardcoded on view.
#2. Searchable, as its now data driven.


这篇关于将视图渲染为字符串以进行站点搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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