.net core 2.2使用taghelpers并传递查询字符串的调用操作

时间:2019-06-18 05:42:17

标签: asp.net-mvc asp.net-mvc-routing asp.net-core-2.2

我在Controller A中执行了一个简单的搜索操作。我知道它不是最有效的方法,但它似乎有效。

[HttpPost, ActionName("Search")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Search(string cellnumberFragment, string clientFragment, string serviceFragment, string deviceFragment, string networkFragment, string serialFragment)
{
    var thirdPartySims = _context.ThirdPartySim.AsQueryable();

    if (!string.IsNullOrWhiteSpace(cellnumberFragment)) thirdPartySims = thirdPartySims.Where(s => s.Cellnumber.Contains(cellnumberFragment));
    if (!string.IsNullOrWhiteSpace(clientFragment)) thirdPartySims = thirdPartySims.Where(s => s.Client.Name.Contains(clientFragment));
    if (!string.IsNullOrWhiteSpace(serviceFragment)) thirdPartySims = thirdPartySims.Where(s => s.Service.Name.Contains(serviceFragment));
    if (!string.IsNullOrWhiteSpace(deviceFragment)) thirdPartySims = thirdPartySims.Where(s => s.DeviceName.Contains(deviceFragment));
    if (!string.IsNullOrWhiteSpace(networkFragment)) thirdPartySims = thirdPartySims.Where(s => s.Network.Name.Contains(networkFragment));
    if (!string.IsNullOrWhiteSpace(serialFragment)) thirdPartySims = thirdPartySims.Where(s => s.Serial.Contains(serialFragment));

    return View(await thirdPartySims.ToListAsync());
}

在控制器B的视图中,我想有一个到搜索控制器的链接,将指定的值传递给幕后的“搜索”。字典使我可以清楚地指定每个动作参数。然后将其与标签帮助程序asp-all-route-data

一起添加
@{
    var parms = new Dictionary<string, string>
    {
        {"cellnumberFragment", null },
        {"clientFragment", Model.Name },
        {"serviceFragment", null },
        {"deviceFragment", null },
        {"networkFragment", null },
        {"serialFragment", null }
    };
}
<a asp-controller="ThirdPartySims" asp-action="Search" asp-all-route-data="parms">List all SIM's for client</a>

这将创建一个类似

的链接
http://localhost:52827/ThirdPartySims/Search?clientFragment=Not%20Specified

当我通过索引视图正常加载“搜索”时,它可以正常工作。通过这种方式运行时,通过使用标签处理程序的链接,我得到

This page isn’t working If the problem continues, contact the site owner.
HTTP ERROR 405

我的vs2019日志说

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: 
Request starting HTTP/1.1 GET 
http://localhost:52827/ThirdPartySims/Search  
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: 
Executing endpoint '405 HTTP Method Not Supported'
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: 
Executed endpoint '405 HTTP Method Not Supported'
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: 
Request finished in 6.3539ms 405 

2 个答案:

答案 0 :(得分:0)

尝试添加[FromQuery]属性

[HttpPost, ActionName("Search")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Search([FromQuery] string cellnumberFragment, string clientFragment, string serviceFragment, string deviceFragment, string networkFragment, string serialFragment)
{
    var thirdPartySims = _context.ThirdPartySim.AsQueryable();

    if (!string.IsNullOrWhiteSpace(cellnumberFragment)) thirdPartySims = thirdPartySims.Where(s => s.Cellnumber.Contains(cellnumberFragment));
    if (!string.IsNullOrWhiteSpace(clientFragment)) thirdPartySims = thirdPartySims.Where(s => s.Client.Name.Contains(clientFragment));
    if (!string.IsNullOrWhiteSpace(serviceFragment)) thirdPartySims = thirdPartySims.Where(s => s.Service.Name.Contains(serviceFragment));
    if (!string.IsNullOrWhiteSpace(deviceFragment)) thirdPartySims = thirdPartySims.Where(s => s.DeviceName.Contains(deviceFragment));
    if (!string.IsNullOrWhiteSpace(networkFragment)) thirdPartySims = thirdPartySims.Where(s => s.Network.Name.Contains(networkFragment));
    if (!string.IsNullOrWhiteSpace(serialFragment)) thirdPartySims = thirdPartySims.Where(s => s.Serial.Contains(serialFragment));

    return View(await thirdPartySims.ToListAsync());
}

答案 1 :(得分:0)

看起来像是发布相关的问题。

  

当我通过索引视图正常加载搜索时

我猜您有一个具有默认post方法的搜索表单,所以这是一个post请求,并且由于您确实有post search方法[HttpPost],因此可以正常工作。

  

以这种方式运行时,通过使用标签处理程序的链接,我得到...

实际上,问题不是标签帮助程序,但是使用链接意味着这是一个获取请求,这就是为什么您遇到错误的原因。

解决方案1:

如果您需要通过链接访问搜索功能,只需将方法标记为get HttpGet并删除防伪令牌验证,这样您就可以将链接与包含在url中的搜索参数一起使用。

[HttpGet, ActionName("Search")]
public async Task<IActionResult> Search([FromQuery] string cellnumberFragment, string clientFragment, string serviceFragment, string deviceFragment, string networkFragment, string serialFragment)
{
    var thirdPartySims = _context.ThirdPartySim.AsQueryable();

    if (!string.IsNullOrWhiteSpace(cellnumberFragment)) thirdPartySims = thirdPartySims.Where(s => s.Cellnumber.Contains(cellnumberFragment));
    if (!string.IsNullOrWhiteSpace(clientFragment)) thirdPartySims = thirdPartySims.Where(s => s.Client.Name.Contains(clientFragment));
    if (!string.IsNullOrWhiteSpace(serviceFragment)) thirdPartySims = thirdPartySims.Where(s => s.Service.Name.Contains(serviceFragment));
    if (!string.IsNullOrWhiteSpace(deviceFragment)) thirdPartySims = thirdPartySims.Where(s => s.DeviceName.Contains(deviceFragment));
    if (!string.IsNullOrWhiteSpace(networkFragment)) thirdPartySims = thirdPartySims.Where(s => s.Network.Name.Contains(networkFragment));
    if (!string.IsNullOrWhiteSpace(serialFragment)) thirdPartySims = thirdPartySims.Where(s => s.Serial.Contains(serialFragment));

    return View(await thirdPartySims.ToListAsync());
}

解决方案2:

只需向搜索功能发出发布请求,如果您仍然需要使用链接,您可能会看到此答案https://stackoverflow.com/a/33880971/5519026