【发布时间】:2023-03-17 20:42:01
【问题描述】:
我在控制器 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
当我通过索引视图正常加载搜索时,它工作正常。当以这种方式运行时,通过使用标签处理程序的链接,我得到 p>
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
【问题讨论】:
-
"当我正常加载搜索时,它工作正常。当以这种方式运行时,我得到"什么是正常的,这种方式是什么?
-
@LazZiya 我已经编辑清楚这个和那个。
标签: asp.net-mvc asp.net-mvc-routing asp.net-core-2.2