开发者

Optional parameter in ASP.net 4.0 c#

I have a website built in ASP.NET 4.0 and currently I have a drop down box with URLs, a button that goes to that URL and parses out text and then finally a results box with the parsed text.

What I want to do is instead of going to my website and choosing the URL in the drop down box, I want to pass on the parameter as a full URL and have the button go to that and then do its thing. Kind of like a permanent link that I can hand to a user

For instance:

http://localhost:62554/WebSite5/Default.aspx --> http://google.com

google.com gets put into a variable (lets say its MyURL) and the button takes it just like if it were in the drop down box. **Updated code: Now getting a error 500 at

using (StreamReader objReader = new StreamReader(objRequest.GetResponse().GetResponseStream()))

string newURL;
String url;

protected void Page_Load(object sender, EventArgs e)
{

    //Request.Params.Get("newURL").ToString();
    //string url = Request["newURL"];
    //url = Request.QueryString["newURL"].ToString();
    url = Request.QueryString["newURL"].ToString();

    parseButton_Click(sender, e);
}
protected void parseButton_Click(object sender, EventArgs e)
{

    //MyUR开发者_高级运维L = deviceCombo.Text;
    //MyURL = Request.Params.Get("");
    //MyURL = Request.Params.Get("newURL");
    //MyURL = newURL;
    //string MyURL = Request.Params["newURL"].ToString();

    WebRequest objRequest = HttpWebRequest.Create(url);
    objRequest.Credentials = CredentialCache.DefaultCredentials;
    using (StreamReader objReader = new StreamReader(objRequest.GetResponse().GetResponseStream()))
    {
        originalText.Text = objReader.ReadToEnd();
    }

    //Read all lines of file
    String[] crString = { "<BR>&nbsp;" };
    String[] aLines = originalText.Text.Split(crString, StringSplitOptions.RemoveEmptyEntries);
    String noHtml = String.Empty;

    for (int x = 0; x < aLines.Length; x++)
    {
        if (aLines[x].Contains(filterCombo.SelectedValue))
        {
            noHtml += (RemoveHTML(aLines[x]) + "\r\n");

        }
    }

    //Print results to textbox
    resultsBox.Text = String.Join(Environment.NewLine, noHtml);
}

Any ideas? Thank you


Your question was hard to understand, but I think what you are trying to do is pass in the website string in the URL. I think what you want to do is use URL params, so if they go to "http://localhost:62554/Website5/default.aspx?newURL=http://google.com" it acts as if they had chosen google.com in the dropdown.

Anything after the ? in the URL is treated as an URL param, you can access these params in the codebehind like this:

string newURL = Request.Params.Get("newURL");


You can get the URL variable from the Request object.

string url = Request["newURL"];

Put this wherever you like such as Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    string url = Request["newURL"];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜