Ich möchte Ruhe API erstellen, aber ich habe Probleme mit dem Routing. Diese URL ist ok: localhost/project/controller/action/id
, aber ich möchte diese URL localhost/project/controller/id
auch. Zum Beispiel: localhost/project/article/5
: ArticleController.Get(int id)
Dies ist mein Code: ProjektcontrollerASP MVC Routing Rest api
[HttpGet]
[Route("project/{id}")]
public JsonResult Get(int id)
{
}
Strecke config:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Api",
url: "{controller}/{id}",
defaults: new { id = UrlParameter.Optional }
);
}
Wenn ich URL localhost/Projekt aufrufen/article/5, Fehlermeldung A Öffentlichkeit Die Aktionsmethode '5' wurde nicht im Controller 'ArticleController' gefunden.
Wo ist das Problem? verwenden Dank
Welche Version von mvc Sie verwenden? – Jamiec