开发者

ASHX C# for each loop on POST vars

开发者 https://www.devze.com 2023-01-21 15:10 出处:网络
I have the following piece of code on C#, on an ASHX or Generic Handler file: public override void ProcessRequest(HttpContext contexto)

I have the following piece of code on C#, on an ASHX or Generic Handler file:

public override void ProcessRequest(HttpContext contexto)
{
    string destino = contexto.Request["destino"];

    string variables = "?";

    string valor = "";

    foreach (string nombre in contexto.Request.QueryString)
    {
        if (nombre == "destino")
        {
            continue;
        } // Fin del if.
        else
        {
            if (contexto.Request.QueryString[nombre] != "")
            {
                valor = contexto.Request.QueryString[nombre];

                variables += nombre + "=" + valor + "&";
            } // Fin del if.
        } // Fin del else.
    } // Fin del foreach.

    variables = variables.Substring(0, variables.Length - 1);

    if (destino != null && destino != "")
    {
        switch (destino)
        {
            case "coordenadasPorMunicipios":    contexto.Response.Redirect("./admon/coordenadasPorMunicipios/CoordenadasPorMunicipiosControl.ashx" + variables);
                                                break;
            case "departamentos":               contexto.Response.Redirect("./admon/departamentos/DepartamentosControl.ashx" + variables);
                                                break;
            case "municipios":                  contexto.Response.Redirect开发者_StackOverflow中文版("./admon/municipios/MunicipiosControl.ashx" + variables);
                                                break;
            case "negocios":                    contexto.Response.Redirect("./admon/negocios/NegociosControl.ashx" + variables);
                                                break;
            case "paises":                      contexto.Response.Redirect("./admon/paises/PaisesControl.ashx" + variables);
                                                break;
            case "sectoresIndustria":           contexto.Response.Redirect("./admon/sectoresIndustria/SectoresIndustriaControl.ashx" + variables);
                                                break;
            case "sectoresIndustriaPorNegocio": contexto.Response.Redirect("./admon/sectoresIndustriaPorNegocio/SectoresIndustriaPorNegocioControl.ashx" + variables);
                                                break;
            case "tiposNegocioPorNegocio":      contexto.Response.Redirect("./admon/tiposNegocioPorNegocio/TiposNegocioPorNegocioControl.ashx" + variables);
                                                break;
            case "tiposNegocios":               contexto.Response.Redirect("./admon/tiposNegocios/TiposNegociosControl.ashx" + variables);
                                                break;
            case "usuarios":                    contexto.Response.Redirect("./admon/usuarios/UsuariosControl.ashx" + variables);
                                                break;
        } // Fin del switch.
    } // Fin del if.
} // Fin del método ProcessRequest.

It works fine for GET vars, I mean, the ones that are sent via URL, but I want to do this for the POST vars.

I tryied to do a for each over he HttpContext object but I get a message that said that HttpContext class didn't have an Enumerator inmpelementation.

Any idea of how can I do this for both GET and POST vars??

Thanks for the help!!


Request.Form is what you are looking for, it's for POST variables


Where Request.QueryString is for the GET, Request.Form is for the form post variables. If you want to enumerate both at the same time, you can use Request.Params


Request.Params gives you a collection of Form, QueryString, Cookie and Server Variables, which is possibly too much, so you'll probably want to limit yourself to iterating over just .Forms and .QueryString.

0

精彩评论

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

关注公众号