【问题标题】:How to route a string in .net core如何在.net核心中路由字符串
【发布时间】:2018-01-03 15:15:53
【问题描述】:

我的 ASP.NET Webb 应用程序中有一个控制器,当我像这样搜索时应该显示搜索结果..

Cases/Search/Volvo

在这种情况下,Volvo 是我搜索的字符串。

目前这样搜索时有效..

Cases/Search?q=volvo

我的路由应该如何工作?

【问题讨论】:

    标签: c# asp.net-core-mvc asp.net-core-2.0 asp.net-core-routing


    【解决方案1】:

    可以考虑使用Attribute路由

    [Route("[controller]"]
    public class CasesController : Controller {
    
        //Matches GET cases/search/volvo
        [HttpGet]
        [Route("search/{q}")]
        public IActionResult Search(string q) {
            //...code removed for brevity
        }
    
    }
    

    参考Routing to Controller Actions

    【讨论】:

    • 如果想要少一行,可以使用[HttpGet("search/{q]")] :)
    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多