0

asp.net と c# のスキルを学び、向上させるために、単純な CMS をプロジェクトとして構築しようとしています。Web フォームをセットアップし、次のコードを使用してコンテンツをデータベースに入力しています。

protected void btnSave_Click(object sender, EventArgs e)
    {
        //Set up connection string.
        const string connectionString = "Data Source=localhost;Initial Catalog=CMSDatabase;Integrated Security=True";

        //SQL statement
        string statement = "INSERT INTO PageContent(Title, Content, Image) VALUES (@title, @content, @image)";
        SqlCommand command = new SqlCommand(statement);

        //Escape special characters
        string content = Regex.Escape(ftbContent.Text);

        //Add contents of form to the database.
        command.Parameters.AddWithValue("@title", txtTitle.Text);
        command.Parameters.AddWithValue("@content", content);
        command.Parameters.AddWithValue("@image", txtImage.Text);

        try
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            command.Connection = connection;
            command.ExecuteNonQuery();
        }
        catch
        {
            //do exception handling stuff
        }
    }

Regex.Escape メソッドを使用して、リッチテキスト エディターによって作成されている html をエスケープしようとしましたが、うまくいきません。私が間違っているところを誰かに説明してもらえますか?

次のサーバー エラーが発生しています: System.Web.HttpRequestValidationException: 潜在的に危険な Request.Form 値がクライアントから検出されました (ftbContent="

ありがとう。

4

3 に答える 3