views:

143

answers:

3

I'm using a controller to call a stored procedure that requires 12 parameters. This works perfectly in debug mode locally (working against a remote database), but not when I publish it to my IIS 7 server. It complains about parameter #7, claiming it's not supplied with the URL.

The URL call looks like this;

http://localhost:50160/GetPlaces?p1=1&p2=1&p3=1&p4=1&p5=1&p6=1&p7=1&p8=1&p9=1&p10=1&p11=1&p12=1

Does anyone have any idea what may be the cause of this? Any help would be very appreciated here.

using System;
using System.Configuration;
using System.Web.Mvc;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Prototype.Models;

namespace Prototype.Controllers
{
    public class NameOfStoredProcedureController : Controller
    {

        char[] lastComma = { ',' };

        StringBuilder json = new StringBuilder();

        private String strCon = ConfigurationManager.ConnectionStrings["SomeConnectionString"].ConnectionString;
        private SqlConnection con;

        public StoredProcedureController()
        {
            con = new SqlConnection(strCon);
        }

        public string do_NameOfStoredProcedure(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9, int p10, int p11, int p12)
        {
            con.Open();

            using (SqlCommand cmd = new SqlCommand("NameOfStoredProcedure", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@parameter1", p1);
                cmd.Parameters.AddWithValue("@parameter2", p2);
                cmd.Parameters.AddWithValue("@parameter3", p3);
                cmd.Parameters.AddWithValue("@parameter4", p4);
                cmd.Parameters.AddWithValue("@parameter5", p5);
                cmd.Parameters.AddWithValue("@parameter6", p6);
                cmd.Parameters.AddWithValue("@parameter7", p7);
                cmd.Parameters.AddWithValue("@parameter8", p8);
                cmd.Parameters.AddWithValue("@parameter9", p9);
                cmd.Parameters.AddWithValue("@parameter10", p10);
                cmd.Parameters.AddWithValue("@parameter11", p11);
                cmd.Parameters.AddWithValue("@parameter12", p12);


                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        json.AppendFormat("[{0},\"{1}\"],", reader["column1"], reader["column2"]);
                    }
                }
                con.Close();
            }

            if (json.Length.ToString().Equals("0"))
            {
                return "[]";
            }

            else
            {
                return "[" + json.ToString().TrimEnd(lastComma) + "]";
            }
        }


        //http://host.com/NameOfStoredProcedure?parameter=value
        public ActionResult Index(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9, int p10, int p11, int p12)
        {
            return new ContentResult
            {
                ContentType = "application/json",
                Content = do_NameOfStoredProcedure(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12)
            };
        }
    }
}
+1  A: 

Have you checked with "GET request length limit"?

portland
Hmm, right. I suppose there is a setting like that in IIS somewhere, I am very amateur at IIS still, but I'll look for it. Thanks for the advice. It just seems a bit odd that such a relatively short url would go over the default limits.
cc0
@portland - I set the content limit up, but still no cigar. I am also not getting any other error when trying to load the page, other than that not all parameters were supplied.
cc0
If you are using IIS 7 or above, there is an attribute called maxQueryString in section requestFiltering that allows you to control that, default value 2048: http://www.iis.net/ConfigReference/system.webServer/security/requestFiltering/requestLimits
CarlosAg
+1  A: 

The code has lots of issues, but none that should be stopping it.

You need to debug.

  1. Put a break point at the start of your controller and check the values of its parameters are being set as expected.

  2. Run SQL Profiler to see what calls are being made to your database, and check it is as you expect.

Richard
Are there any issues with this code except the apparent unit-testing incompability?The parameters from p7 and out does not get assigned values from the URL, the 6 first ones are ok. Again, this all works locally, so it seems there is an IIS issue at hand.
cc0
Code issues: (1) Both `con` and `json` fields should be locals. (2) Naming. (3) Directly exposing the SProc to a HTTP GET. If I can immediately see three I assume there are likely deeper issues that would show up with deeper study or more context.
Richard
Thank you, good feedback. What exactly are the naming issues you can see though?
cc0
@cc0: e.g. name "do_NameOfStoredProcedure" for a public method (and why is it public?) and of its parameters. The naming here is less significant than as an approach to design (issues #1 and #3 in my earlier comment).
Richard
@Richard - Got it. Thanks for the advice, I'll make changes accordingly. Working on moving more of the code into separate classes.
cc0
A: 

Set the MaxRequestBytes in http parameters to 64kb

cc0