如何在ASP.NET MVC中路由表单



当我用搜索参数搜索产品时,我想重定向url/products/search/或/products/id/search。

routes.MapRoute(
name: "products",
url: "products/{id}/{search}/{page}",
defaults: new { controller = "products", action = "Index", id = UrlParameter.Optional, page = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index",id = UrlParameter.Optional }
);

当我在显示/products的搜索框url中搜索产品时?search=参数名称。但是当点击菜单项url显示/products/search时。

将[HttpGet("{id:int}"(]添加到控制器中方法的顶部
像这样:

[HttpGet("{id:int}")]
public virtual async Task<ApiResult<List<PostShortSelectDto>>> GetSimilar(CancellationToken cancellationToken, int id)
{
return await _postRepository.GetSimilar(cancellationToken, id);
}

现在路由器是这样的:
/Controller/GetSimulate/1

如果使用.net mvc,请使用以下代码:

[HttpGet]
[Route("GetSimilar/{id}")]
public virtual async Task<ApiResult<List<PostShortSelectDto>>> GetSimilar(CancellationToken cancellationToken, int id)
{
return await _postRepository.GetSimilar(cancellationToken, id);
}

最新更新