如何在c#中将部分url作为变量传递?

时间:2013-07-29 15:00:58

标签: c# url

我有以下网址:http://localhost:52416/Controls/Support_Survey.aspx?GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9

我需要从URL获取GUID作为变量,并将其传递给以下存储过程:

 database.InsertUpdate(String.Format("CALL spSurveyAnswer_Insert('{0}', '{1}','{2}');", selectValue1, txtFeedBack.Text, PassGUID_HERE));

有任何想法吗?

提前致谢

5 个答案:

答案 0 :(得分:6)

以下是我建议你这样做的方法:

var requestGuid = Request.Params["GUID"];

if (string.IsNullOrEmpty(requestGuid))
{
    throw new InvalidOperationException("The request GUID is missing from the URL");
}

Guid guid;

if (!Guid.TryParse(requestGuid, out guid))
{
    throw new InvalidOperationException("The request GUID in the URL is not correctly formatted");
}

using(var connection = new SqlConnection("connection_string"))
{
    using(var command = new SqlCommand("spSurveyAnswer_Insert", connection))
    {
        command.CommandType = CommandType.StoredProcedure;        
        command.Parameters.AddWithValue("firstParamName", selectValue1);
        command.Parameters.AddWithValue("feedbackParamName", txtFeedBack.Text);
        command.Parameters.AddWithValue("guidParamName", guid);

        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

您无法保证GUID将在URL 中成为有效的GUID,因此请采取防御措施并检查两者!然后使用参数化查询来帮助防止SQL注入 - 因为您正在调用存储过程,如果您滥用proc中的参数值,则可以注入SQL,因此您也需要仔细编写。最后,还要妥善处理一次性资源。

答案 1 :(得分:3)

您应该使用RequestParamsQueryString(请参阅他们的文档来了解差异)来获取GUID,出于安全考虑,您应该在所有SQL中使用参数命令和查询,而不是字符串连接/格式化。我正在使用CommandType.StoredProcedure允许的简化语法。参数名称("firstParamName"等)应与存储过程中声明的实际参数名称匹配。

Guid myGuid = new Guid(Request.Params["GUID"]);

using (SqlConnection conn = // get connection)
using (SqlCommand command = new SqlCommand("spSurveyAnswer_Insert", conn))
{
    conn.Open();
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.AddWithValue("firstParamName", selectValue1);
    command.Parameters.AddWithValue("feedbackParamName", txtFeedBack.Text);
    command.Parameters.AddWithValue("guidParamName", myGuid);

    command.ExecuteNonQuery();
}

答案 2 :(得分:0)

这应该这样做:

Guid myGuid = new Guid(Request.Params["GUID"])

将其作为实际Guid进行投射也可以防止SQL注入攻击

答案 3 :(得分:0)

string url = "http://localhost:52416/Controls/Support_Survey.aspx?GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9";
string lastPart = url.Split('?').Last().Replace("GUID=",string.Empty);

您的代码是对SQL注入的探测,因此请使用SqlCommand.Parameters Property

 SqlCommand command = // your sql command;
    database.InsertUpdate(String.Format("CALL spSurveyAnswer_Insert('{0}', '{1}','{2}');", @selectValue1, @txtFeedBack, @PassGUID_HERE));

    command.Parameters.AddWithValue("@selectValue1", selectValue1);
    command.Parameters.AddWithValue("@txtFeedBack", txtFeedBack.Text);
    command.Parameters.AddWithValue("@PassGUID_HERE", lastPart );

答案 4 :(得分:-1)

Uri url = new Uri("http://localhost:52416/Controls/Support_Survey.aspx?GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9");

string query = url.Query //query is now "GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9"

string guidStr = query.Replace("GUID=", "");
Guid guid = new Guid(guidStr);
相关问题