简体   繁体   English

处理ASP.NET中的传入请求URL

[英]Handling Incoming Request URLs in ASP.NET

Currently trying to augment the search of an existing ASP.NET site. 目前正在尝试扩充现有ASP.NET站点的搜索。 My background is Java, so if I was writing a server in Java, I could just handle the incoming request as a string and parse it however I wanted, taking out the search terms, etc. 我的背景是Java,所以如果我用Java编写服务器,我可以将传入的请求作为字符串处理,然后根据需要解析它,取出搜索条件等。

What part of ASP handles that? ASP的哪个部分处理? Where should I be looking for where the incoming string is taken apart to handle the search request? 我应该在哪里寻找传入字符串被拆分以处理搜索请求的位置? There is a search button which redirects the page to a URL which includes the search parameters. 有一个搜索按钮,可将页面重定向到包含搜索参数的URL。 That's where the trail goes cold for me as I need to know where it comes back into the server. 这就是我的路径变冷,因为我需要知道它回到服务器的哪个位置。

For example, once you've vetted the search term it gets submitted like this: 例如,一旦您审核了搜索字词,就会像这样提交:

Response.Redirect("~/shop?" + type + "=" + searchBoxContent);

'type' is the type of search so that could be based on brand or searching within the product description, etc. 'type'是搜索的类型,因此可以基于品牌或在产品描述中搜索等。

The site is already using some type of url rewriting as the url doesn't show up with any .aspx when you do a search. 该网站已经在使用某种类型的网址重写,因为当您进行搜索时,网址不会显示任何.aspx。 Should I be looking in a config file or a .master.cs file or where to point me in the right direction? 我应该查看配置文件或.master.cs文件,还是指向正确的方向?

The easiest system for this is ASP.NET MVC which has a built-in Route and parameter handler. 最简单的系统是ASP.NET MVC,它有一个内置的Route和参数处理程序。

See MSDN for docs. 有关文档,请参阅MSDN

Example: 例:

{controller}/{action}/{id}

Can redirect to a Controller action: 可以重定向到Controller操作:

public ActionResult Find(int id)
{ ... }

If this is not what you want, take a look at this blog article of Scott Guthrie on URL rewriting. 如果这不是您想要的,请查看Scott Guthrie关于URL重写的博客文章

If you have a URL like this: 如果您有这样的网址:

/shop.aspx?type=abc

then you can use Request.QueryString the get the value of type . 然后你可以使用Request.QueryString获取type的值。 This is the syntax: 这是语法:

Request.QueryString["type"]

For example if you want to get the value when /shop.aspx?type=abc is loaded at the first time, then you should add this code in Page_Load method inside the code behind ( shop.aspx.cs ): 例如,如果您想在第一次加载/shop.aspx?type=abc时获取值,那么您应该在后面的代码( shop.aspx.cs )中的Page_Load方法中添加此代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // assign 'type' query string to typeOfSearch
        string typeOfSearch = Request.QueryString["type"];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM