How do I setup routing to support this?
If I switch the routes around, then GetSomeStuff works, but then /api/values does not work. How do I configure route to have them both work?
Example methods:
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
// GET api/values/5
[HttpGet]
public string GetSomeStuff(int id)
{
return "stuff";
}
Routes are setup like this:
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
How about instead of:
GET /api/values/GetSomeStuff/1
you designed your urls like that:
GET /api/someStuff/1
Now you could simply have a SomeStuffController
and a Get(int id)
action on it.
You could try the following routes:
config.Routes.MapHttpRoute(
name: "DefaultController",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = @"^[0-9]*$" }
);
config.Routes.MapHttpRoute(
name: "DefaultController2",
routeTemplate: "api/{controller}/{action}/{id2}"
);
and change your action method to:
[HttpGet]
public string GetSomeStuff(int id2)
{
return "stuff";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.