问题描述
在我的MasterPage上,我使用TextBox和Button以这种方式在网站上搜索书籍:
MasterPage上的标记:
On my MasterPage I'm using a TextBox and a Button to search for books on site in this way:
Markup on MasterPage:
<asp:TextBox runat="server" ID="txtSearch" placeholder="Search" />
<asp:Button runat="server" ID="btnSubmit" Text="Search" OnClick="btnSubmit_Click" />
代码落后:
Code behind:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Redirect("search.aspx?book=" + txtSearch.Text);
}
因此,当用户键入书名并单击搜索时,他将被重定向到search.aspx页面搜索结果附加到URL。
现在在search.aspx上Page_Load我正在使用这段代码来获取这本书:
So when a user type a book title and click search, he is redirected to the search.aspx page with the search results appended to the URL.
Now on search.aspx Page_Load I'm using this code to get the book:
String searchQuery = Request.QueryString["book"];
using (ELibraryEntities entities = new ELibraryEntities())
{
var search = from books in entities.Reviews
where books.Title.Contains(searchQuery)
select books;
ListView1.DataSource = search;
ListView1.DataBind();
}
如果我只搜索站点根目录中的页面,一切正常。如果我从这样的文件夹中的页面搜索:http:// localhost:52747 / contacts / about.aspx,它显示:找不到资源。请求的URL:/contacts/search.aspx。
看起来MasterPage上的btnSubmit_Click事件有问题吗?!
Everything works fine if I only search from pages that are on the root of the site. If I search from pages that are on folders like this: http://localhost:52747/contacts/about.aspx, it's showing: The resource cannot be found. Requested URL: /contacts/search.aspx.
Looks like a problem on the btnSubmit_Click event on the MasterPage?!
推荐答案
Response.Redirect("~/search.aspx?book=" + txtSearch.Text);
问题是该语句在相对路径内重定向到搜索页面。因此,当你是一个深层结构的文件夹时,它会尝试在同一级别找到页面并最终失败。
因此,你需要进入根级别搜索页面驻留。
希望这会有所帮助!
The problem is that the statement redirects to the search page within relative path. So, when you are one folder deep in structure it tries to find the page on same level and eventually fails.
Thus, you need to go to root level where the search page resides.
Hope this helps!
这篇关于“无法找到资源”在网站上搜索时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!