【问题标题】:ASP.NET Core route form get URLASP.NET Core 路由表单获取 URL
【发布时间】:2021-02-01 17:24:08
【问题描述】:

我在启动时设置了以下路线..

endpoints.MapControllerRoute(
    name: "areas",
    pattern: "{area=Admin}/{controller=Home}/{action=Index}/{id?}");

endpoints.MapControllerRoute("pagination",
    "Inventory/Page/{page}",
    new { area = "Admin", controller = "Inventory", action = "Index", page = 1 });

这些适用于以下网址:

/Admin/Inventory/Edit/6
/Inventory/Page/2

我在InventoryController 中定义了以下操作方法:

[HttpGet]
public async Task<IActionResult> Index(int page = 1)
{
    var pageInfo = new PagingInfo()
    {
        CurrentPage = page,
        ItemsPerPage = PageSize,
        TotalItem = await _inventoryRepository.CountAsync(new InventorySpecification())
    };

    var model = new InventoryListViewModel()
    {
        Inventories = await _inventoryRepository.GetAsync(new InventorySpecification(page, PageSize)),
        PagingInfo = pageInfo
    };

    return View(model);
}

[HttpGet]
public async Task<IActionResult> Search(string filter, int page = 1)
{
    if (String.IsNullOrEmpty(filter))
    {
        RedirectToAction(nameof(Index));
    }

    var pageInfo = new PagingInfo()
    {
        CurrentPage = page,
        ItemsPerPage = PageSize,
        TotalItem = await _inventoryRepository.CountAsync(new InventorySpecification(filter))
    };

    var model = new InventoryListViewModel()
    {
        Inventories = await _inventoryRepository.GetAsync(new InventorySpecification(filter, page, PageSize)),
        PagingInfo = pageInfo
    };

    return View(model);
}

我有这个视图模型类:

public class InventoryListViewModel : BaseListViewModel
{
    public IEnumerable<Inventory> Inventories { get; set; }
    public string SearchString { get; set; }
}

我在 Index.cshtml 中有以下 razor 语法

<div class="row">
        <div class="col-12">
            <form asp-action="Search" method="get">
                <div class="input-group">
                    <input asp-for="SearchString" type="text" class="form-control" name="filter" placeholder="Enter an inventory item">
                    <span class="input-group-btn">
                        <button class="btn btn-success" type="submit">Go!</button>
                    </span>
                </div>
            </form>
        </div>
    </div>

当用户提交表单时,我希望看到 ....

/Inventory/Search/{filter}/Page/1

我在启动时尝试了几条路线,但都没有成功。

任何想法或建议将不胜感激。

【问题讨论】:

  • 可以通过浏览器输入url查看是否可以访问吗?
  • 我可以输入 URL 并且它可以工作...我有 endpoints.MapControllerRoute(inventory", "Inventory/Search/{filter}/Page/{page}", new { area = "Admin", controller = "Inventory", action = "Search", filter="All", page=1}); 问题是当我提交表单时,我没有得到想要的 URL。我得到../ Inventory/Search/All/Page?filter=somevalue

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


【解决方案1】:

在 SO 上进行了更多搜索后,我偶然发现了这个 post...

我在启动中有这条路线...

endpoints.MapControllerRoute("inventory",
"Inventory/Search/{filter}/Page/{page}",
new { area = "Admin", controller = "Inventory", action = "Search", filter="All", page=1});

我的库存控制器的以下条目..

[HttpPost]
public async Task<IActionResult> SearchPost(string filter, int page=1)
{
   return await Task.Run<IActionResult>(() =>
   {
     return RedirectToAction(nameof(Search), new { filter = filter, page = page });
   });
}
    
[HttpGet]
public async Task<IActionResult> Search(string filter, int page)
{
   if (String.IsNullOrEmpty(filter))
   {
     RedirectToAction(nameof(Index));
   }
    
   var pageInfo = new PagingInfo()
   {
      CurrentPage = page,
      ItemsPerPage = PageSize,
      TotalItem = await _inventoryRepository.CountAsync(new InventorySpecification(filter))
   };
    
   var model = new InventoryListViewModel()
   {
     Inventories = await _inventoryRepository.GetAsync(new InventorySpecification(filter, page, PageSize)),
     PagingInfo = pageInfo
     };
    
     return View(model);
}

我对表单进行了简单的调整,使其成为 post vs get 并引用 SearchPost 方法。

  <form asp-action="SearchPost" method="post">
                <div class="input-group">
                    <input asp-for="SearchString" type="text" class="form-control" id="filter" name="filter" placeholder="Enter a server name or instance name">
                    <span class="input-group-btn">
                        <button class="btn btn-success" type="submit">Go!</button>
                    </span>
                </div>
            </form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2016-02-22
    • 2010-09-21
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多