У меня есть вопрос.
У меня есть таблица пользователей.Идентификатор пользователя 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;
}
Когда я вызываю 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."
}
Мой вопрос заключается в том, как сделать метод GetWatchedProduct
каккогда id
равно int
(GetWatchedProduct(int? id)
)?Можно ли сделать то же самое со строкой?Мне нужен другой метод Get?
РЕДАКТИРОВАНИЕ:
Когда я вызываю мой API со строковым параметром:
http://localhost.../api/WatchedProduct/StringidHere
Это работает, я хочу иметь один метод для GET вмой контроллерКогда String.Empty
и когда я передаю строку.
My 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 }
);
}
}
EDIT2
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 }
);
}
EDIT3:
Например, этот метод с другого контроллера работает:
// 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
, когда я захочу.