【问题标题】:HTTP get request possible without passing very long string to URL?无需向 URL 传递很长的字符串即可获得 HTTP 请求?
【发布时间】:2019-10-20 09:47:09
【问题描述】:

传递很长的 GET 请求最有效的方法是什么?我看到在这里发布了一个类似的问题:Very long http get request 建议使用发布请求而不是获取?这是为什么呢?

例如,我有一个包含 4 个多选列表和一个下拉列表的应用程序。用户可以选择几个选项来过滤表格并显示结果。目前,这包含在我的 onget 方法中,方法是在 URL 中构建一个字符串,然后运行 ​​linq 查询以根据用户的选择显示结果。它有效,但对我来说传递这么长的 URL 似乎效率很低。没有更好的方法来使用模型绑定吗?

.cshtml 文件:

<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 文件:

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)); }
}

【问题讨论】:

    标签: asp.net-core model-binding razor-pages


    【解决方案1】:

    模型绑定适用于 GET 请求和 POST 请求。您只需要确保公共属性用BindProperty 属性和SupportsGet = true (https://www.learnrazorpages.com/razor-pages/model-binding#binding-data-from-get-requests) 进行装饰:

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

    对于很长的 URL,有一个limit to the length of a GET request。 POST 请求也有服务器施加的限制,但默认情况下要大得多。例如,它需要满足文件上传的需要。

    【讨论】:

    • 是的,我忘了提到我已经尝试过了,但它不适合我。但是,我想知道这是否是因为在我的 html 文件中,我没有将那些选择列表包含在:
      中。这可能是为什么,还是应该如此?
    • 表单提交将仅包含来自表单内控件的值,所以是的,如果您的选择元素不在表单内,则在提交表单时,它们选择的值将不会包含在有效负载中。
    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    相关资源
    最近更新 更多