我有一个问题。
我有一个用户表。用户ID为=WORKDAY(A1; 5; DATE(2018; 8; 15))
。我有看起来像这样的GET:
string
当我在Postman中调用API时,得到以下响应:
// GET: api/WatchedProduct
[HttpGet]
public IEnumerable<WatchedProduct> GetWatchedProduct(string id)
{
var productsList = id == String.Empty ?
db.WatchedProducts.Where(u => u.ApplicationUserId == id).ToList() :
db.WatchedProducts.Where(u => u.ApplicationUserId == loggedUserId).ToList();
return productsList;
}
我的问题是,如何使{
"message": "No HTTP resource was found that matches the request URI 'http://.../api/WatchedProduct'.",
"messageDetail": "No action was found on the controller 'WatchedProduct' that matches the request."
}
是GetWatchedProduct
(id
)时的int
方法?是否可以对字符串执行相同操作?我需要另一个Get方法吗?
编辑:
当我使用字符串参数调用API时:
GetWatchedProduct(int? id)
它有效,我想在控制器中使用一种GET方法。当http://localhost.../api/WatchedProduct/StringidHere
以及我传递字符串时。
我的String.Empty
RouteConfig.cs
EDIT2
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
//AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
WebApiConfig.cs
EDIT3:
例如,另一个控制器的方法起作用:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
/*var cors = new EnableCorsAttribute("http://localhost:4200", "*", "GET,POST,PUT,DELETE,OPTIONS");
cors.SupportsCredentials = true;
config.EnableCors(cors);*/
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
但就我而言,我想有一种方法可以调用 // GET: api/Product/search?str=kaarol
[Route("search")]
public IEnumerable<Product> GetSearch(string str)
{
return db.Products.Where(p => p.Name.Contains(str)).ToList();
}
并在需要时使用/Api/Watched...
。
答案 0 :(得分:4)
通常,您的路由类似于GET /api/controllerName/
,如果您要像GET /api/controllerName/customRoute
那样访问它,则需要对方法进行路由。
// GET: api/WatchedProduct
[HttpGet]
[Route("WatchedProduct")]
public IEnumerable<WatchedProduct> GetWatchedProduct(string id)
{
var productsList = id == String.Empty ?
db.WatchedProducts.Where(u => u.ApplicationUserId == id).ToList() :
db.WatchedProducts.Where(u => u.ApplicationUserId == loggedUserId).ToList();
return productsList;
}
如果您的控制器名称为WatchedProductController
,则无需显式指定它。我的意思是:
public class WatchedProduct : ApiController
{
// GET: api/WatchedProduct
[HttpGet]
public IEnumerable<WatchedProduct> Get(string id)
{
var productsList = id == String.Empty ?
db.WatchedProducts.Where(u => u.ApplicationUserId == id).ToList() :
db.WatchedProducts.Where(u => u.ApplicationUserId == loggedUserId).ToList();
return productsList;
}
}
应该可以。
答案 1 :(得分:0)
您可以尝试这样的事情
public class WatchedProduct : ApiController
{
// GET: api/WatchedProduct
[HttpGet]
[Route("WatchedProduct")]
public IEnumerable<WatchedProduct> Get()
{
var context = Request.Properties["MS_HttpContext"] as HttpContext;
var id = context.Request.QueryString["id"];
var productsList = id == String.Empty ?
db.WatchedProducts.Where(u => u.ApplicationUserId == id).ToList() :
db.WatchedProducts.Where(u => u.ApplicationUserId == loggedUserId).ToList();
return productsList;
}
}
在满足这两种路线的情况下均应适用于这两种情况。