Getting Bad Request with a large url

I’m receveing a Bad Request when i call a large url.

https://localhost:44320/RespostaEmail/96635/750396/[%7B%22IdItem%22:8,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:1,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:3,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:2,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:5,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:7,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:10,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:4,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:6,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:9,%22IdTipoReposta%22:80%7D]

I redirect to this url when i call this function in javascript

function responder(idDivergencia, numGF, dia, mes, ano) {
let observacao = $('#observacao').val();

if (observacao.trim() == '') {
    abrirDialogAlertaMensagem('Informe uma observação para visualizar a tela de resposta.');
    return;
}

while (observacao.indexOf("https://stackoverflow.com/") != -1) {
    observacao = observacao.replace("https://stackoverflow.com/", '-')
}

var lista = ObterListaDeItens(idDivergencia, numGF);
var itens;

if (lista != null) {

    $('.btnSalvarDiv').click();
    itens = JSON.stringify(lista);

    window.location.href = RELATIVE_PATH + 'RespostaEmail/' + idDivergencia + "https://stackoverflow.com/" + numGF + "https://stackoverflow.com/" + itens
} else {
    abrirDialogAlertaMensagem('Informe o(s) tipo(s) de resposta para visualizar a tela de resposta.');
    return;
}

}

and that’s my controller

[HttpGet]
    [Route("RespostaEmail/{idDivergencia}/{numeroGf}/{itensDivergencia}")]
    public IActionResult RespostaEmail(int idDivergencia, int numeroGf, string itensDivergencia)
    {
        if (itensDivergencia == "favicon.ico")
            return View();

        var listaItems = JsonConvert.DeserializeObject<List<ItemResposta>>(itensDivergencia);

        var breadcrumb = BreadcrumbControl.GetInstance();
        breadcrumb.AddNewPage("Resposta Email", "Resposta", "RespostaEmail");

        var dados = new Resposta();
        var itens = _divergenciaAppService.ListarItensDivergencia(idDivergencia);

        foreach (var item in itens)
        {
            dados.ItemSolicitado.Add(new ItemSolicitado
            {
                CodigoDivergencia = item.CodDivergencia,
                IdGrupo = item.IdGrupo,
                IdItem = item.IdItem,
                Peca = item.PecaSolicitada,
                Quantidade = item.QtdDivergente ?? 0,
                TipoResposta = listaItems.FirstOrDefault(x => x.IdItem == item.IdItem).IdTipoReposta
            });
        }

        dados.IdDivergencia = idDivergencia;
        dados.NumeroGF = numeroGf;

        var email = _respostaAppService.MontarEmailResposta(dados);

        ViewBag.TextoPadrao = email;
        ViewBag.IdDivergencia = idDivergencia;
        ViewBag.Conta = ContaUsuario;
        ViewBag.IdGrupo = dados.ItemSolicitado.Select(x => x.IdGrupo).LastOrDefault();
        return View();
    }

I am trying to configure the max length but i’m not finding where to config this, my project is in .netcore 3.1. That has a Startup, but i cannot find where i can solve this problem. Or someone has other ways to deal with this problem.

  • There is a limit for url length. But for the query string, which is way much larger you can change it to a query string.

    – 

  • That string really isn’t all that long: you should be able to get to about 2000 characters before running into issues. How sure are you that this is a length problem? Does it work correctly if you have fewer items? The server may be choking on the bracket characters: your javascript is not encoding the list–use encodeURIComponent() to encode that URI segment.

    – 

Leave a Comment