【发布时间】:2020-04-07 14:30:08
【问题描述】:
我有 2 个页面可以通过这些操作访问:
public class SearchEngineController : Controller
{
[Route("/search/{k}")]
public IActionResult Search(string k = "")
{
return View();
}
}
public class ChannelController : Controller
{
[Route("{name}")]
public IActionResult Index(string name = "")
{
return View();
}
}
现在,当我使用键 (somekey) 搜索内容时,我想重定向到 url localhost:5000/search?k=somekey
因为我们使用的是频道(比如 Youtube 的频道),所以我们需要对频道名称进行分类,它应该是唯一的。例如,名称为mobifone 的频道可以通过localhost:5000/mobifone 访问。
在调用搜索请求时,直到 name 参数(在 Index 操作内)无法分类之前,一切可能看起来都不错。所以,每次我输入localhost:5000/search?k=somekey,它都会触发Index 动作。
所以,我的临时解决方案如下:
public class ChannelController : Controller
{
[Route("{name}")]
public IActionResult Index(string name = "")
{
if (name.ToLower() == "search")
{
// ~/Views/Shared/Search.cshtml
return View("Search");
}
return View();
}
}
它可以解决问题但是....我不喜欢它。因为我不想在ChannelController 中嵌套和执行搜索查询。它不是频道的一部分。一个频道可能包含:
- Id
- Name
- DisplayName
- FounderId
- ...
在中间件中,_channelManager 不应该有一个搜索引擎成员可以返回世界上的一切,例如:
- Channel information
- List of channels
- User profile
- A post content
- List of posts
- ...
有没有比我更好的方法?
【问题讨论】:
-
从 `[Route("/search/{k}")]` 中删除
/和{k}时对我有用
标签: c# asp.net-mvc asp.net-core routes