Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You must disambiguate your methods by having two different action names. </p> <p>What you can do to have a RESTful API is to create your own <code>RouteHandler</code> class which will change the routing, dependent on whether or not the {id} value is present. The RouteHandler class is simple:</p> <pre><code>using System.Web.Mvc; using System.Web.Routing; public class MyRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { var routeData = requestContext.RouteData; if (routeData.Values("id").ToString() == "0" /* our default invalid value */ ) { var action = routeData.Values("action"); routeData.Values("action") = action + "Add"; } var handler = new MvcHandler(requestContext); return handler; } } </code></pre> <p>Next, modify <code>RegisterRoutes()</code> in Global.asax.cs as follows:</p> <pre><code> public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); AreaRegistration.RegisterAllAreas(); routes.Add("Default", new Route("{controller}/{action}/{id}", new RouteValueDictionary( new { controller = "Home", action = "Index", id = "0" /* our default invalid value */ }), new MyRouteHandler())); } </code></pre> <p>Finally, change the <code>ActionName</code> attribute for your Add method to "IndexAdd". When the request comes in without an id, the default invalid value will be supplied, which will alert your RouteHandler to change the method to your "Add" method.</p> <p>counsellorben</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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