在 QueryString 中指定 returnURL

Specify returnURL In QueryString

这可能是一个简单的问题,但我正在为查询字符串中的 returnurl 而苦苦挣扎。我知道如何将查询字符串中的 returnurl 调用为 Response.Redirect,但我不确定如何将 returnurl 设置为某个 url。有人可以举例说明如何执行此操作吗?

您可以按照以下方式进行:

var url = Request.Url.ToString();
var uri = String.Format("http://example.com?page={0}", url);
Response.Redirect(uri);

代码非常简单。

我有一个建议给你,我确定它适合你的情况。

让我定义一个 Static Dictionary<string,string> 来保存一些密钥和相应的 URLs。由于它是静态定义的,您可以从所有其他页面访问它,因此该变量将获得应用程序范围。即,

public static Dictionary<string, string> URLDictonary = new Dictionary<string, string>()
                                         {
                                          {"google","http://google.com/"}, 
                                          {"dotnet","http://www.dotnetperls.com/"},     
                                          {"querystring","http://www.dotnetperls.com/querystring"}
                                         };

这样您就可以将 key 名称与 URL 作为查询字符串附加在一起。它可能如下所示:

Response.Redirect("~/Somepage.aspx?returnURL=google");
// Which means you are passing the key as query string

现在您可以在示例页面中获取此key并根据密钥重定向到特定页面,如下所示:

string returnURL = Request.QueryString["returnURL"];
if (returnURL != null)
{
    Response.Redirect(URLDictonary[returnURL]);
}

因为我们传递 google 它将重定向到相应的值即。 "http://google.com/".

注意:您可以使用自己的键和 Url 创建类似的字典。如果它在不同的 class 中定义,则使用 class_name.DictonaryName[querystring_value]