1

What is the most efficient way to pass a very long GET request? I see a similar question was posted here: Very long http get request which suggests using a post request instead of get? Why is this?

As an example, I have an application that includes 4 multiselectlists and a dropdown. The user can select several options to filter a table down and display the results. Currently this is included in my onget method by building a string in the URL and then running linq queries to display the results based on the user's selections. It works, but it seems very inefficient to me to pass such a long URL. Isn't there a better way to do this with model binding?

.cshtml file:

<select multiple class="form-control" name="CurSelDepts" asp-items="Model.DeptList" asp-for="SelDepts"></select>
<select multiple class="form-control" name="CurSelTechs" asp-items="Model.TechOneList" asp-for="SelTechs"></select>
<select multiple class="form-control" name="CurSelTech2s" asp-items="Model.TechTwoList" asp-for="SelTech2s"></select>
<select multiple class="form-control" name="CurSelRoles" asp-items="Model.RoleList" asp-for="SelRoles"></select>
<select class="form-control col-md-6" name="CurSelEmp" asp-items="Model.EmployeeList" asp-for="SelEmp">
    <option disabled selected style="display:none">--select--</option>
</select>
<input formmethod="get" type="submit" value="Search" class="btn btn-primary btn-sm" id="searchbtn" />

.cs file:

public MultiSelectList DeptList { get; set; }
public MultiSelectList TechOneList { get; set; }
public MultiSelectList TechTwoList { get; set; }
public SelectList EmployeeList { get; set; }
public MultiSelectList RoleList { get; set; }
public int SelEmp { get; set; }
public int SelNewEmp { get; set; }
public int[] SelRoles { get; set; }
public int[] SelDepts { get; set; }
public int[] SelTechs { get; set; }
public int[] SelTech2s { get; set; }

public async Task OnGetAsync(int[] selRoles, int[] curSelRoles, int selEmp, int curSelEmp, int[] selDepts, int[] curSelDepts, int[] selTechs, int[] curSelTechs, int[] selTech2s, int[] curSelTech2s)
{
    DeptList = new MultiSelectList(_context.ppcc_deptCds, "Id", "dept_cd", SelDepts);
    TechOneList = new MultiSelectList(_context.ppcc_techCds, "Id", "tech_cd", SelTechs);
    TechTwoList = new MultiSelectList(_context.ppcc_techTwoCds, "Id", "tech_cd_two", SelTech2s);
    RoleList = new MultiSelectList(_context.ppcc_roles, "Id", "role_nm", SelRoles);
    EmployeeList = new SelectList(_context.employees, "Id", "employee_nm", SelEmp);

    SelEmp = curSelEmp;
    SelDepts = curSelDepts;
    SelTechs = curSelTechs;
    SelTech2s = curSelTech2s;
    SelRoles = curSelRoles;

    IQueryable<ppcc_matrix> ppcc_matrixIQ = from s in _context.ppcc_matrices select s;

    if (curSelDepts.Any())  {ppcc_matrixIQ = ppcc_matrixIQ.Where(s => curSelDepts.Contains(s.ppcc_deptCdId));}
    if (curSelTechs.Any())  {ppcc_matrixIQ = ppcc_matrixIQ.Where(s => curSelTechs.Contains(s.ppcc_techCdId));}
    if (curSelTech2s.Any()) {ppcc_matrixIQ = ppcc_matrixIQ.Where(s => curSelTech2s.Contains(s.ppcc_techTwoCdId));}
    if (curSelRoles.Any())  {ppcc_matrixIQ = ppcc_matrixIQ.Where(s => curSelRoles.Contains(s.ppcc_roleId));}
    if (curSelEmp != 0) { ppcc_matrixIQ = ppcc_matrixIQ.Where(s => s.employeeId.Equals(curSelEmp)); }
}
mstiver2018
  • 107
  • 9

1 Answers1

1

Model Binding works with GET requests as well as POST requests. You just need to ensure that the public properties are decorated with the BindProperty attribute with SupportsGet = true (https://www.learnrazorpages.com/razor-pages/model-binding#binding-data-from-get-requests):

[BindProperty(SupportsGet=true)]
public Type MyProperty { get; set; }

As to very long URLs, there is a limit to the length of a GET request. POST requests also have limits imposed by servers, but it is very much larger by default. It needs to be to cater for file uploads, for example.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • Yes, I neglected to mention that I've already tried this and it isn't working for me. However, I'm wondering if that's because in my html file, I don't have those select lists wrapped in:
    . Could this be why, or should that matter?
    – mstiver2018 Jun 21 '19 at 17:59
  • A form submission will only include values from controls within a form, so yes, if your select elements are not inside a form, their selected values will not be included in the payload when the form is submitted. – Mike Brind Jun 21 '19 at 19:43