[英]ASP WebApi GET response with string id?
我有个问题。
我有一个用户表。 用户ID是一个string
。 我有看起来像这样的GET:
// 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;
}
当我在Postman中调用API时,得到以下响应:
{
"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."
}
我的问题是,如何使id
为int
( GetWatchedProduct(int? id)
)的GetWatchedProduct
方法? 是否可以对字符串执行相同操作? 我需要另一个Get方法吗?
编辑:
当我使用字符串参数调用API时:
http://localhost.../api/WatchedProduct/StringidHere
它有效,我想在控制器中使用一种GET方法。 当String.Empty
以及我传递字符串时。
我的RouteConfig.cs
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 }
);
}
}
编辑2
WebApiConfig.cs
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 }
);
}
编辑3:
例如,来自另一个控制器的该方法有效:
// 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...
并在需要时使用StringID
。
通常,您的路由就像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;
}
更多信息Microsoft文档在这里
如果您的控制器名称为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;
}
}
应该可以。
您可以尝试这样的事情
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;
}
}
因为它满足了两条路线,所以在两种情况下都应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.