Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC Web Api Route with default action not working
    primarykey
    data
    text
    <p>In my <code>TestController</code> I have the following:</p> <pre><code> [HttpGet] public IEnumerable&lt;String&gt; Active() { var result = new List&lt;string&gt; { "active1", "active2" }; return result; } [HttpGet] public String Active(int id) { var result = new List&lt;string&gt; { "active1", "active2" }; return result[id]; } </code></pre> <p>In <code>RouteConfig</code> the mapping is:</p> <pre><code> 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 }); </code></pre> <p>In a browser the following request works:</p> <pre><code>api/test/active/1 </code></pre> <p>But this returns a <strong>Internal Server Error</strong>:</p> <pre><code>api/test/active </code></pre> <p>What do you have to do to return a <em>action</em> that may or maynot have a parameter in a similar manner to the default Get?</p> <p><strong>Update 1</strong> As Cuong Le suggested, changing the ordering of routes helped, the routes are now:</p> <pre><code> 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 } ); </code></pre> <p>I had to remove <code>action = ""</code> from the ActionApi route otherwise the standard Get on the other controllers stopped working (i.e. api/values)</p> <p>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"?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload