在asp.net中读取查询字符串而不指定任何页面名称

时间:2021-07-13 15:30:14

How to read any strings in a aspx page.

如何读取aspx页面中的任何字符串。

example: http://foo.com/bike stand

例如:http://foo.com/bike stand

I want to read/get the strings in a specified aspx page. expected page string is bike stand
expected page is getstring.aspx

我想在指定的aspx页面中读取/获取字符串。预期页面字符串是自行车站预期页面是getstring.aspx

Here i like to read the string and redirect to specified page.
Note: I like to do this in just ASP.Net (Not with MVC)

在这里,我想读取字符串并重定向到指定的页面。注意:我喜欢在ASP.Net(不使用MVC)中执行此操作

3 个解决方案

#1


4  

You can probably solve this using a Route. I have made a simple demo, that you can try out in just a couple of minutes. In the Global.asax.cs file, add this method:

您可以使用Route解决此问题。我做了一个简单的演示,你可以在几分钟内试用。在Global.asax.cs文件中,添加以下方法:

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Products",
        "Products/{product}", "~/getstring.aspx",
        false,
        new RouteValueDictionary { { "product", "NoneSelected" } }
    );
}

In the same file, in the already existing void Application_Start(object sender, EventArgs e) method, add RegisterRoutes(RouteTable.Routes);:

在同一个文件中,在已经存在的void Application_Start(object sender,EventArgs e)方法中,添加RegisterRoutes(RouteTable.Routes);:

void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

With this, you have configured a Route which will take a request like this:

有了这个,你已经配置了一个Route,它将接受这样的请求:

http://foo.com/Products/bike%20stand

and map it to getstring.aspx. Note that I have url encoded the space in the url.

并将其映射到getstring.aspx。请注意,我有url编码url中的空格。

In getstring.aspx you can access the value ("bike stand") like this:

在getstring.aspx中,您可以像这样访问值(“自行车站”):

protected void Page_Load(object sender, EventArgs e)
{
    string routeValue = "";
    if (RouteData.Values.ContainsKey("product"))
        routeValue = RouteData.Values["product"].ToString();

    //routeValue now contains "bike stand"

    SelectedProduct.Text = routeValue;
}

I have set up the Route in this sample on the path "Products" beneath the application folder. I don't recommend that you setup your route directly under the application folder as in the question. You can do it though if you absolutely want to:

我已在此示例中的应用程序文件夹下的“Products”路径中设置了Route。我不建议您直接在应用程序文件夹下设置路径,如问题所示。如果你绝对想要,你可以这样做:

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Products",
        "{product}", "~/getstring.aspx",
        false,
        new RouteValueDictionary { { "product", "NoneSelected" } }
    );
}

#2


3  

It may not the best way to fulfill your need but if you want to get the string part from URL you may use URI Segments.

它可能不是满足您需求的最佳方式,但如果您想从URL获取字符串部分,则可以使用URI段。

HttpContext.Current.Request.Url.Segments.Select(x=>x.TrimEnd‌​('/')).Skip(2).ToArr‌​ay();

OR

new Uri("http://www.someurl.com/foo/xyz/index.htm#extra_text").Segments

Results: [ "/", "foo/", "xyz/", "index.html" ]

结果:[“/”,“foo /”,“xyz /”,“index.html”]

After reading string you can do whatever you want like redirection etc

读完字符串后,你可以做任何你想要的重定向等

#3


2  

You could use the IIS URL Rewrite Module for this. When installed you can create a rule in the Web.Config System.Webserver node that rewrites the url so the string can be processed in code behind as a QueryString.

您可以使用IIS URL重写模块。安装后,您可以在Web.Config System.Webserver节点中创建一个规则,该节点重写URL,以便可以在后面的代码中将字符串作为QueryString处理。

<rewrite>
  <rules>
    <rule name="getString" stopProcessing="true">
      <match url="^([a-z0-9- /]+)$" ignoreCase="true"/>
      <action type="Rewrite" url="getstring.aspx?string={R:1}"/>
    </rule>      
  </rules>
</rewrite>

Then you can simply get the string value with this:

然后你可以简单地用这个得到字符串值:

string getString = Request.QueryString["string"];

But his will create a problem if there are other pages than getstring.aspx than need to be accessed since every request is now being send to getstring.aspx. Maybe it's better to use a prefix so that you can identify the request URL as being one that has a string.

但是,如果除了getstring.aspx之外还有其他页面需要访问,那么他会产生一个问题,因为现在每个请求都被发送到getstring.aspx。也许最好使用前缀,以便您可以将请求URL标识为具有字符串的URL。

http://foo.com/getstring/bike stand

<match url="^getstring/([a-z0-9- /]+)$" ignoreCase="true"/>

#1


4  

You can probably solve this using a Route. I have made a simple demo, that you can try out in just a couple of minutes. In the Global.asax.cs file, add this method:

您可以使用Route解决此问题。我做了一个简单的演示,你可以在几分钟内试用。在Global.asax.cs文件中,添加以下方法:

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Products",
        "Products/{product}", "~/getstring.aspx",
        false,
        new RouteValueDictionary { { "product", "NoneSelected" } }
    );
}

In the same file, in the already existing void Application_Start(object sender, EventArgs e) method, add RegisterRoutes(RouteTable.Routes);:

在同一个文件中,在已经存在的void Application_Start(object sender,EventArgs e)方法中,添加RegisterRoutes(RouteTable.Routes);:

void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

With this, you have configured a Route which will take a request like this:

有了这个,你已经配置了一个Route,它将接受这样的请求:

http://foo.com/Products/bike%20stand

and map it to getstring.aspx. Note that I have url encoded the space in the url.

并将其映射到getstring.aspx。请注意,我有url编码url中的空格。

In getstring.aspx you can access the value ("bike stand") like this:

在getstring.aspx中,您可以像这样访问值(“自行车站”):

protected void Page_Load(object sender, EventArgs e)
{
    string routeValue = "";
    if (RouteData.Values.ContainsKey("product"))
        routeValue = RouteData.Values["product"].ToString();

    //routeValue now contains "bike stand"

    SelectedProduct.Text = routeValue;
}

I have set up the Route in this sample on the path "Products" beneath the application folder. I don't recommend that you setup your route directly under the application folder as in the question. You can do it though if you absolutely want to:

我已在此示例中的应用程序文件夹下的“Products”路径中设置了Route。我不建议您直接在应用程序文件夹下设置路径,如问题所示。如果你绝对想要,你可以这样做:

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Products",
        "{product}", "~/getstring.aspx",
        false,
        new RouteValueDictionary { { "product", "NoneSelected" } }
    );
}

#2


3  

It may not the best way to fulfill your need but if you want to get the string part from URL you may use URI Segments.

它可能不是满足您需求的最佳方式,但如果您想从URL获取字符串部分,则可以使用URI段。

HttpContext.Current.Request.Url.Segments.Select(x=>x.TrimEnd‌​('/')).Skip(2).ToArr‌​ay();

OR

new Uri("http://www.someurl.com/foo/xyz/index.htm#extra_text").Segments

Results: [ "/", "foo/", "xyz/", "index.html" ]

结果:[“/”,“foo /”,“xyz /”,“index.html”]

After reading string you can do whatever you want like redirection etc

读完字符串后,你可以做任何你想要的重定向等

#3


2  

You could use the IIS URL Rewrite Module for this. When installed you can create a rule in the Web.Config System.Webserver node that rewrites the url so the string can be processed in code behind as a QueryString.

您可以使用IIS URL重写模块。安装后,您可以在Web.Config System.Webserver节点中创建一个规则,该节点重写URL,以便可以在后面的代码中将字符串作为QueryString处理。

<rewrite>
  <rules>
    <rule name="getString" stopProcessing="true">
      <match url="^([a-z0-9- /]+)$" ignoreCase="true"/>
      <action type="Rewrite" url="getstring.aspx?string={R:1}"/>
    </rule>      
  </rules>
</rewrite>

Then you can simply get the string value with this:

然后你可以简单地用这个得到字符串值:

string getString = Request.QueryString["string"];

But his will create a problem if there are other pages than getstring.aspx than need to be accessed since every request is now being send to getstring.aspx. Maybe it's better to use a prefix so that you can identify the request URL as being one that has a string.

但是,如果除了getstring.aspx之外还有其他页面需要访问,那么他会产生一个问题,因为现在每个请求都被发送到getstring.aspx。也许最好使用前缀,以便您可以将请求URL标识为具有字符串的URL。

http://foo.com/getstring/bike stand

<match url="^getstring/([a-z0-9- /]+)$" ignoreCase="true"/>