如何在ASP.NET MVC应用程序中实现搜索功能[英] How to implement search features in ASP.NET MVC applications

本文是小编为大家收集整理的关于如何在ASP.NET MVC应用程序中实现搜索功能的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我可以想象在ASP.NET MVC应用程序中插入搜索功能的许多方法,但是由于我找不到太多文档,因此我想知道您是否有任何常见的模式,技术或常用方法可以在ASP中实现搜索功能. NET MVC应用程序(类似于StackoverFlow).我想到的一些技术是:

  • SQL Server全文搜索
  • 外部搜索引擎(如搜索服务器2008)
  • lucene.net

...但是将它们与ASP.NET MVC集成的最佳方法是什么?

想法?

推荐答案

目前尚不完全清楚您要问什么,但总的来说:

  1. 编写返回搜索表格的视图助手或部分视图.在您需要显示搜索框的任何地方都会在其他页面中调用该页面.使表格动作获得,而不是发布.
  2. 对于网站搜索,您可能需要一个搜索控制器.要在一种特定类型的数据中进行搜索,您可以将操作添加到现有控制器或参数中,为现有操作.在大多数情况下,我们唯一要添加的是对特定数据类型的通用"列表"操作的参数.搜索表格调用"列表",并用搜索查询字符串设置一个参数.
  3. 实际搜索是在您的存储库中完成的.这是应用程序中唯一了解SQL Server或Lucene之类的知识的部分.对于琐碎的情况,控制器可以将一个.

其他推荐答案

我相信他的博客文章之一杰夫·阿特伍德(Jeff Atwood)谈到了他如何使用站点地图为了让Google处理堆栈溢出上的大多数搜索功能.当人们可能只是要使用Google时,为什么要编写自己的搜索算法?

本文地址:https://www.itbaoku.cn/post/627754.html

问题描述

I can imagine many ways of implemeting search features in an ASP.NET MVC application but since I can't find much documentation I was wondering if you have any common pattern, technology or common approach to implement search features in a ASP.NET MVC application (similar to stackoverflow). Some technologies that I have in mind are:

  • SQL Server full-text search
  • External search engine (like Search Server 2008)
  • Lucene.NET

...but what is the best approach to integrate them with ASP.NET MVC?

Ideas?

推荐答案

It's not entirely clear what you are specifically asking, but, in general:

  1. Write a view helper or partial view which returns a search form. Call that within your other pages wherever you need to display a search box. Make the form action GET, not POST.
  2. For a site search, you'll probably want to have a search controller. For searching within one particular type of data, you can add an action to an existing controller or an argument to an existing action. For the most part, the only thing that we have to add is an argument to the general-purpose "List" action for a specific datatype. The search form calls "List" and sets an argument with the search query string.
  3. The actual searching is done within your Repository. That's the only part of the application which knows about things like SQL Server or Lucene. For trivial cases a controller could append a .Where to an IQueryable<T> returned by a Repository.

其他推荐答案

I believe in one of his blog posts Jeff Atwood talks about how he used sitemaps in order to let google handle most of the searching capabilities on stack overflow. Why write your own searching algorithms when people are likely just going to use google anyway?