【问题标题】:Asp.Net Core - How to set the UrlAction in Controller?Asp.Net Core - 如何在控制器中设置 UrlAction?
【发布时间】:2021-11-21 02:52:23
【问题描述】:

我有一个输入框要搜索...

HTML:

<form id="searchForm" asp-controller="Product" asp-action="SearchProduct" method="get">

此输入以两种方式执行搜索:

1- 按下 Enter 键后 2-点击“a”标签后

第一步,地址如下图所示:

第二步,地址如下图所示:

再次搜索后,地址如下图所示

JavaScript:

 $('.txt-search-story').click(function () {
    var span = $(this).find('span');
    var txtspan = span.text();

     $.ajax({
         url: "/Product/GetTextSearch",
         type: 'Get',
         data: { "q": txtspan },
         success: function (response) {
             window.location.href = response.redirectToUrl;
         }
    });
});

控制器一:

[Route("/search/{**q}")] 
public async Task<IActionResult> SearchProduct(string q, int page = 1)
    {
        if (q != null)
        {                
            var searchProduct = await _product.SearchProduct(q, skip, countproduct);
            ViewBag.searchtext = q;

            return View(searchProduct);
        }                    
    }

 

Ajax 控制器二:

 public IActionResult GetTextSearch(string q)
    {
        return Json(new { redirectToUrl = Url.Action("SearchProduct", "Product", new { q = q }) });
    }

我希望第二个地址和第一个地址一样我该怎么办?

【问题讨论】:

  • 从路由中删除 /{**q} 它将创建查询 ...?q=a ?

标签: javascript jquery ajax asp.net-core


【解决方案1】:

感谢@daremachine- 我改变了 [Route("/search/{**q}")]

[路线(“/搜索”)]

成功了

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多