0

I'd like have access to a method other than "GET", "PUSH", "PATCH", ....

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "Employee",
            routeTemplate: "api/employee/{employeeid}",
            defaults: new { controller = "employee", employeeid = RouteParameter.Optional }
        );

        //for test : not work
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { action = "get", id = RouteParameter.Optional }
        );

        //JSON Formatting
        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =  Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.Formatters.Remove(config.Formatters.XmlFormatter);
        config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }
}

I have access to the employee controller :

    [RoutePrefix("api/employee")]
    public class EmployeeController : ApiController
    {
        public HttpResponseMessage Get() { }
        public HttpResponseMessage Get(int employeeid) {}
        public HttpResponseMessage Post([FromBody] EmployeeModel model){}

        [HttpPut]
        [HttpPatch]
        public HttpResponseMessage Patch([FromBody] EmployeeModel model){}

        [Route("initialisation")]
        public HttpResponseMessage Initialisation() {}
    }

I have access without any problem :

http://localhost/employee
http://localhost/employee/1

I'd like have access to the "Initialisation" method :

http://localhost/employee/initialisation

I added the route "DefaultApi" but when I try I get this error :

{
  "$id": "1",
  "message": "The request is invalid.",
  "messageDetail": "The parameters dictionary contains a null entry for parameter 'employeeid' of non-nullable type 'System.Int32' 
  for method 'System.Net.Http.HttpResponseMessage Get(Int32)' in 'Pme.WebApi.Controllers.EmployeeController'. 
  An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
}

Thanks,

TheBoubou
  • 19,487
  • 54
  • 148
  • 236

3 Answers3

0

I think this should work out. You can try modifying your WebApiConfig like this :

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                //routeTemplate: "api/{controller}/{id}",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

This way, you keep it open for both, the controller name and the action name to be something dynamic and try hitting the URL that would be generated as per this format.

UPDATE :

Comment the "Employee" route code. Just keep the "DefaultApi" route active. Now try to hit this URL :

http://localhost:1955/api/Employee/Create/parameters

You obviously need to keep the host name as per yours and the parameter too.

Hope this helps.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
  • I already tried, not solve the problem. See the error I added in my post. I didn't hit the breakpoint in the controller – TheBoubou Apr 27 '15 at 07:04
  • @Kris-I - Just updated the answer. Try it again. Also keep in mind what Puneet suggested below. – Dhrumil Apr 27 '15 at 07:13
  • I need the "Employee" route, I create some URL based on the route and not harcoded (even partially) everywhere in the code. – TheBoubou Apr 27 '15 at 07:18
  • Try creating a route in the same manner as above but with Employee details, i.e controller name, action name and parameter name. But in the above format. – Dhrumil Apr 27 '15 at 07:21
0

Try the following:

Change

config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { action = "get", id = RouteParameter.Optional }
    );

To

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{employeeid}",
            defaults: new { action = "get", employeeid= RouteParameter.Optional }
        );

The only change the name of last ID parameter from "id" to "employeeid"

Puneet
  • 2,051
  • 9
  • 17
0

You need to register attribute routing in your WebApiConfig

config.MapHttpAttributeRoutes();

See Attribute Routing in ASP.NET Web API 2

Alternatively you can specify exactly where your route should go

config.Routes.MapHttpRoute("Initialisation", "api/employee/initialisation",
                new {controller = "employee", action = "initialisation"});
JConstantine
  • 3,980
  • 1
  • 33
  • 46