錯誤內容:
Message=未找到與請求 URI“http://localhost:42914/api/Products/Login”匹配的 HTTP 資源。
MessageDetail=在控制器“Products”上找不到與該請求匹配的操作。
原因:
Post類型只能傳遞一個參數,且只能從Body中獲取(加[FromBody]前綴)。
解決方案:
多個參數傳遞時需要放到實體類中,利用實體類獲取傳遞過來的參數。
例:分頁查詢產品信息
實體類:
public class ProductsPage { public int PageIndex { get; set; } public int PageSize { get; set; } public string strWhere { get; set; } public int TotalCount { get; set; } public List<ProductStore.Models.Product> ProductList { get; set; } }
接口方法:
public ProductsPage PostAllProductsByPage([FromBody]ProductsPage model) { model.ProductList = new List<Product>(); model.ProductList = repository.GetAllByPage(model.strWhere, model.PageIndex, model.PageSize).ToList(); model.TotalCount = repository.GetTotalCount(model.strWhere); model.PageIndex = model.PageIndex; model.PageSize = model.PageSize; return model; }