MVC Web Api Route with default action not working - c#

In my TestController I have the following:
[HttpGet]
public IEnumerable<String> Active()
{
var result = new List<string> { "active1", "active2" };
return result;
}
[HttpGet]
public String Active(int id)
{
var result = new List<string> { "active1", "active2" };
return result[id];
}
In RouteConfig the mapping is:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "", id = RouteParameter.Optional });
In a browser the following request works:
api/test/active/1
But this returns a Internal Server Error:
api/test/active
What do you have to do to return a action that may or maynot have a parameter in a similar manner to the default Get?
Update 1
As Cuong Le suggested, changing the ordering of routes helped, the routes are now:
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I had to remove action = "" from the ActionApi route otherwise the standard Get on the other controllers stopped working (i.e. api/values)
api/test/active is now resolving, but I now get a 500 Internal Server Error for /api/test is it possile to have both resolves, so api/test would return "all" and /test/active only return "some"?

It is probably getting confused since you have two methods named action. Try deleting or renaming one of them and see if that works.

One way to do it is to provide a default value for the parameter,
[HttpGet]
public String Active(int id = 0)
{
var result = new List<string> { "active1", "active2" };
if (id == 0) {
return result;
} else {
return result[id];
}
}

Related

C# webAPI restrict route

in a webapi project's WebAPIConfig.cs, 2 routes are added
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I try to create an apiController contains below functions
[HttpGet]
public string Get(int id)
{
return "get";
}
[HttpGet]
[ActionName("ByWait")]
public string[] ByWait(int id)
{
return "bywait";
}
I expects that
requesting /api/controllername/1234 returns "get", and
requesting /api/controllername/bywait/1234 returns "bywait".
However, the actual result is
/api/controllername/1234 >> throw exception Multiple actions were found that match the request
/api/controllername/bywait/1234 >> "by wait"
However can fix the issue?
s.t how to restrict the function ByWait only accepts request containing action so that it only response to /api/controllername/bywait/1234 and ignore /api/controllername/1234
Or there is other better solution?
Thanks
First you can change WebApiConfig:
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}"
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Then controller:
[HttpGet]
public string Get()
{
return "get-default";
}
[HttpGet]
public string Get(int id)
{
return "get" + id;
}
[HttpGet]
[Route("api/values/bywait/{id}")]
public string ByWait(int id)
{
return "bywait";
}

Attribute routing with empty parameter

I created the following route in my RouteConfig.cs
config.Routes.MapHttpRoute(
name: "CustomFilter",
routeTemplate: "api/{controller}/{category}/{begin},{pageSize}",
defaults: new
{
category = RouteParameter.Optional,
begin = RouteParameter.Optional,
take = RouteParameter.Optional
}
);
That is used by the method below:
public IHttpActionResult GetStudentsByCategory(string category, int begin, int pageSize)
{
..
}
The custom route works fine, unless the category parameter is missing
1. api/students/tech/1,3 (is working)
2. api/students//1,3 (not working)
Is it possible to make the second URL request (without a category parameter) work?
Add one more Route without category, it should be above current one
config.Routes.MapHttpRoute(
name: "CustomFilter-without-cat",
routeTemplate: "api/{controller}/{begin},{pageSize}",
defaults: new
{
begin = RouteParameter.Optional,
take = RouteParameter.Optional
}
);
config.Routes.MapHttpRoute(
name: "CustomFilter",
routeTemplate: "api/{controller}/{category}/{begin},{pageSize}",
defaults: new
{
category = RouteParameter.Optional,
begin = RouteParameter.Optional,
take = RouteParameter.Optional
}
);

ASP.NET Web Api url design

I try to use Web Api in an ASP.NET MVC4 application.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ActionIdApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
public class OrganisationApiController : ApiController
{
public List<Direction> GetDirections()
{
List<Direction> res = new List<Direction>();
using (SerializerContext context = new SerializerContext())
{
res = context.DirectionSet.ToList();
}
return res;
}
public List<Departement> GetDepartements(int directionId)
{
List<Departement> res = new List<Departement>();
using (SerializerContext context = new SerializerContext())
{
res = context.DepartementSet.Where(d => d.IdDirection == directionId).ToList();
}
return res;
}
}
Application starts with
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
When I develop I can call all my methods from OrganisationApiController :
http://localhost:5000/api/OrganisationApi => GetDirections is called
http://localhost:5000/api/OrganisationApi/GetDirections => GetDirections is called
http://localhost:5000/api/OrganisationApi/GetDepartements/1 => GetDepartement is called with directionId = 1
But when I deploy on a server with IIS
http://myserver.com:5000/api/OrganisationApi => GetDirections is called
http://myserver.com:5000/api/OrganisationApi/GetDirections => GetDirections is called
http://myserver.com:5000/api/OrganisationApi/GetDepartements/1 => 404 error
What I am missing?
You have a problem with those routes:
config.Routes.MapHttpRoute(
name: "ActionIdApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
The second route is unnecessary (because the first one already handles those requests - id is specified as optional) and it has an error that you specifying that id is optional but the route doesn't contain an id at all.
Either remove the second route and change your action so it receive an optional parameter or replace them with:
config.Routes.MapHttpRoute(
name: "ActionIdApi",
routeTemplate: "api/{controller}/{action}/{id}",
);
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}",
);
By the way your default rote is also very problematic because you are not specifying default controller and action - it supposed to be DEFAULT right...
From what i see here all your routes could be replaced with this one
config.Routes.MapHttpRoute(
name: "ApiDefault",
routeTemplate: "api/{controller}/{action}/{id}",
new { controller = "HomeApi", action="IndexApi" id = RouteParameter.Optional });
Just replace your default values for controller and action.
EDIT:
The problem is in your action:
If you specify in your Route "id" parameter your action should be:
public List<Departement> GetDepartements(int id)
Parameter names must match.
Also you can combine both your actions into one action:
public List<Departement> GetDepartements(int? id)
{
List<Departement> res = new List<Departement>();
using (SerializerContext context = new SerializerContext())
{
if(id.HasValue)
{
res = context.DepartementSet.Where(d => d.IdDirection == directionId).ToList();
}
else
{
res = context.DirectionSet.ToList();
}
}
return res;
}

Is there a way to tell routing that my default action name is equal to the HTTP Action Verb?

I've looked through this document on MSDN and can't come up with the answer.
Considering that I have a route defined like this:
config.Routes.MapHttpRoute(
name: "DefaultWithActionAndID",
routeTemplate: "v{version}/{controller}/{action}/{id}",
defaults: null,
constraints: new {
action = #"[a-zA-Z]+",
id = #"\d+"
}
);
config.Routes.MapHttpRoute(
name: "DefaultWithID",
routeTemplate: "v{version}/{controller}/{id}",
defaults: null,
constraints: new {
id = #"\d+"
}
);
config.Routes.MapHttpRoute(
name: "DefaultWithoutActionOrId",
routeTemplate: "v{version}/{controller}",
defaults: null,
);
Now I have two controllers that looks like this:
public class ItemController:ApiController{
[HttpGet]
public Item Get(int id){}
[HttpGet]
public Item GetSomething(int id){}
[HttpPut]
public Item Put(Item newItem){}
}
public class AnotherController:ApiController{
[HttpPut]
public HttpResponseMessage Put(Something item){}
}
I'd like to be able to call all of these endpoints like this:
GET /api/Item/344
GET /api/Item?id=344
GET /api/Item/Something/2334
GET /api/Item/Something?id=2334
PUT /api/Item body={newItem}
PUT /api/Another body={newSomething}
This will work, but only if I add "Get" as the default action name. If I do not specify a default action name in my route, then it complains about multiple matching action names. If I do add the default action name, then I cannot call PUT to the Put() method without an error because the action name doesn't match the default and isn't found.
// Will work in some cases, but not all
config.Routes.MapHttpRoute(
name: "DefaultWithID",
routeTemplate: "v{version}/{controller}/{id}",
defaults: new {
action="Get",
id=RouteParameters.Optional
},
constraints: new {
id = #"\d+"
}
);
// Works
GET /api/Item/344
GET /api/Item?id=344
GET /api/Item/Something/2334
GET /api/Item/Something?id=2334
// Doesn't work
PUT /api/Item body={newItem}
PUT /api/Another body={newSomething}
How can I tell Routing to use the Action with the name that matches my HTTP Verb, if one exists before trying to use
If you define your routes as follows:
config.Routes.MapHttpRoute(
name: "DefaultWithActionAndID",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new {action = #"[a-zA-Z]+", id = #"\d*" }
);
config.Routes.MapHttpRoute(
name: "DefaultWithID",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GET", id = RouteParameter.Optional },
constraints: new { id = #"\d*", httpMethod = new HttpMethodConstraint(new string[] { "GET" }) }
);
config.Routes.MapHttpRoute(
name: "DefaultWithoutActionOrId",
routeTemplate: "api/{controller}",
defaults: new { action = "PUT" },
constraints: new { httpMethod = new HttpMethodConstraint(new string[] { "PUT" }) }
);
And also place the ActionName attribute on your GetSomething method as so:
[ActionName("Something")]
public Item GetSomething(int id){}
You should then be able to hit all the endpoints mentioned above.
The way I see it you'd need the following setup:
1.
/api/Item/344
{controller}/{id}
2.
/api/Item/Something/2334
{controller}/{action}/{id}
and decorate the 'GetSomething' method as follows:
[ActionName("Something")]
public Item GetSomething(int id){}
3.
/api/Item?id=344
/api/Item/Something?id=2334
I'm not entirely sure about these - have you tried adding a default to the routes above:
defaults: new { id = RouteParameter.Optional }
4.
I'd expect PUT to just work if #3 is applied
Let me know if that changes anything.

Routing optional parameters

I'm working on my first web api and I'm stuck trying to create rules for these urls
1) http://mydomain.com/values/4 --> this number can be any, this is just an example
2) http://mydomain.com/values/
3) http://mydomain.com/values/?param1=test1&param2=test2
4) http://mydomain.com/values/?param1=test1
5) http://mydomain.com/values/?param2=test2
My current routing rules are
routes.MapHttpRoute(
name: "Route1",
routeTemplate: "{controller}/{id}/"
);
routes.MapHttpRoute(
name: "Route2",
routeTemplate: "{controller}/{param1}/{param2}",
defaults: new { param1 = RouteParameter.Optional, param2 = RouteParameter.Optional }
);
And my methods serving these urls
// GET values/
// GET values/?param1=test1&param2=test2
// GET values/?param1=test1
// GET values/?param2=test2
public IEnumerable Get(string param1, string param2)
{
return new string[] { "value1", "value2" };
}
// GET values/5
public string Get(int id)
{
return "value";
}
I have 2 problems
1) http://mydomain.com/values/ is not resolving
2) http://mydomain.com/values/?param1=test1 and http://mydomain.com/values/?param2=test2 are not resolving.
Could you help me to create routes for serving these urls?
routes.MapHttpRoute(
name: "Route1",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Should do what you need. Query string parameters are already mapped to Action parameters as long as they are named the same, no need to add them in the route.
First you don't need the route
routes.MapHttpRoute(
name: "Route2",
routeTemplate: "{controller}/{param1}/{param2}",
defaults: new { param1 = RouteParameter.Optional, param2 = RouteParameter.Optional }
);
Secondly to resolve http://mydomain.com/values/ you need to make parameter id optional by making it int?
// GET values/5
public string Get(int? id)
{
return "value";
}

Categories

Resources