WebAPI C# Routing - c#

I have two end points:
api/v1/user/session (For creating user login with post request )
api/v1/user (For creating user with post request)
How to route this two endpoints in same controller? I also want to specify action for a specific request. More clearly:
all get,post,update, patch operations can be done in api/v1/user/session endpoint
all get,post,update, patch operations can be done in api/v1/user endpoint
Is it possible ?
Example:
config.Routes.MapHttpRoute(
"UserApi",
"api/v1/{controller}/session",
new { controller = "User", action="Session" });
Now, I want all rest requests to work for Session method with [httpPost],[httpGet] etc attributes.
config.Routes.MapHttpRoute("lol", "api/v1/{controller}/session",
new { controller = "User", action="Session" });
//config.Routes.MapHttpRoute(
// name: "LoginApi",
// routeTemplate: "api/v1/{controller}",
// defaults: new { controller = "User"}
//);
config.Routes.MapHttpRoute(
name: "RailStationApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

I would suggest you look at attribute routing - this is a lot easier to specify than using the central configuration.
[RoutePrefix("api/v1")]
public class UserController : ApiController {
[HttpPost]
[Route("user/session")]
public void Login(/*...*/) {
// ...
}
[HttpGet]
[Route("user/session")] // Note this has the same route as Login
public SessionResult GetSession(/*...*/) {
// ...
}
[HttpPost]
[Route("user")]
public void CreateUser(/*...*/) {
// ...
}
}
Note that you don't technically need [HttpPost] since it is the default, but I included it for clarity. You can add methods with the other Http verbs in the same way.

I tried with hardcoded solution and it worked. I added following route into the webapi.config file and it worked.
RouteTable.Routes.MapHttpRoute(
name: "SessionApi",
routeTemplate: "api/v1/user/session",
defaults: new { Controller = "Session", id = RouteParameter.Optional }
).RouteHandler = new SessionStateRouteHandler();

Related

C# REST API Controller: same route with 2 different actions

When using the following routes:
config.Routes.MapHttpRoute(
name: "new_device",
routeTemplate: "api/v1/devices",
defaults: new { controller = "Devices", action = "new_device" }
);
config.Routes.MapHttpRoute(
name: "devices_list",
routeTemplate: "api/v1/devices",
defaults: new { controller = "Devices", action = "devices_list", httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
The controller looks as follows:
public class DevicesController : ApiController
{
[HttpPost]
[ResponseType(typeof(IHttpActionResult))]
[Route("api/v1/devices")]
[ActionName("new_device")]
[ValidateModel]
public IHttpActionResult NewDevice([System.Web.Http.FromBody] Device device )
{
...
}
[HttpGet]
[ResponseType(typeof(IHttpActionResult))]
[Route("api/v1/devices")]
[ActionName("devices_list")]
[ValidateModel]
public List<Device> GetAllDevices()
{
...
}
My expectation would be that the router would find the correct route based on the HttpMethod used since even it's using the same URI it is using a different HttpMethod.
But instead it fails with the following:
"Message": "The requested resource does not support http method 'GET'."
My guess is because it fins a match with the URI and then checks if the method if the same.
Is there a way to achieve using the same URI with different Http Method which is by the way REST guidelines? Am I missing something?
Ok , I check your whole code. I think you are trying to achieve the calls in complicated way.
Following code is for the configuration :
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
and follwoing is your controller code :
public class DevicesController : ApiController
{
[HttpPost]
[ResponseType(typeof(IHttpActionResult))]
[ActionName("newDevice")]
public IHttpActionResult NewDevice([System.Web.Http.FromBody] Device device)
{
return null;
}
[HttpGet]
[ResponseType(typeof(IHttpActionResult))]
[ActionName("devices_list")]
public List<Device> GetAllDevices()
{
return null;
}
}
I removed ValidateModel. I think it's your custom attribute or somehow related with built in nuget package.
Anyways, execute the calls with Postman or any HTTP client tool. It should work , as it was working at my end with above mentioned code.
Example Calls:
https://localhost:44370/api/v1/devices/devices_list = > Get.
https://localhost:44370/api/v1/devices/newDevice => Post. Provide body as post call for the object.

ASP.NET Web Api configuring a specific route not executing message handler

I am trying to configure a specific route to execute a message handler before it hits the controller, however the message handler is never executed.
I have the following controller:
public class TestController : ApiController
{
[Route("private/users")]
public IHttpActionResult Get()
{
return Ok();
}
}
And the following route configuration:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "private/users",
defaults: new { id = RouteParameter.Optional },
constraints: null,
handler: new TokenValidationHandler() { InnerHandler = new HttpControllerDispatcher(config) }
);
And The MessageHandler looks like this:
public class TokenValidationHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
//some logic to validate token
return base.SendAsync(request, cancellationToken);
}
}
The TokenValidationHandler is never executed which seems to be because of the routeTemplate when configuring the route. Is it possible to achieve route specific configuration with a hardcoded routeTemplate like "private/users" instead of the default "{controller}/{id}" format?
What I want to do is have my TokenValidationHandler fire for all of my private api calls and have public api calls bypass the TokenValidationHandler.
Attribute routes a matched before convention-based routes. The action in question is tagged with an Route attribute. If attribute routing is enabled then it means that it will not reach the convention-based route and thus not invoke the handler.
Remove the route attribute
public class TestController : ApiController {
public IHttpActionResult Get() {
return Ok();
}
}
The order of route registration also plays a factor for convention-based routes so make sure that general routes are registered after more specific routes
config.Routes.MapHttpRoute(
name: "PrivateApi",
routeTemplate: "private/users",
defaults: new { id = RouteParameter.Optional },
constraints: null,
handler: new TokenValidationHandler() { InnerHandler = new HttpControllerDispatcher(config) }
);
//more general route
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
//...
);
Otherwise if attribute routing is your preference you can consider using an action filter.
Based on stated intentions you would be better off using Authentication Filters in ASP.NET Web API 2

Multiple actions were found that match the request when using actions in Route config

I'm currently building an API using Web API 2.2
I have the RESTful part of it working but now I need one non-RESTful controller:
public class PremisesController : ApiController
{
private PremiseService _service;
public PremisesController()
{
_service = new PremiseService();
}
[HttpGet]
public IHttpActionResult Premise(string id)
{
id = id.Replace(" ", String.Empty).ToUpper();
List<Premise> premises = _service.GetPremisesForPostcode(id);
return Ok(premises);
}
[HttpGet]
public IHttpActionResult Building(string id)
{
double premise = Convert.ToDouble(id);
Building building = _service.GetBuildingsForPremise(premise);
return Ok(building);
}
}
The routing config is as follows:
config.MapHttpAttributeRoutes();
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 }
);
Im getting the error that it can't distinguish between the two methods when I initiate a GET action:
Multiple actions were found that match the request
So my question is Do I need to specify the Route attribute on top of each method and if yes, why? Doesn't the second route (ActionApi) deals with that situation?
EDIT:
I just tested you're code and it works the way it is... maybe just it is unclear.
/api/Premises/Premise/8 --> will take you to your first action
/api/Premises/Building/8 --> will take you to your second action
/api/Premises/8 --> will cause error because the routing will go to the first rule api/{controller}/{id} with a GET request, then he can't distinguish which of the actions you want because they both match the first route: (api/Premises/{id})
You could also use the RoutePrefix attribute on your controller.
[RoutePrefix("api/premises")]
public class PremisesController : ApiController
That combined with the route attribute would mean you shouldn't get multiple actions with the same route

How to add custom methods to ASP.NET WebAPI controller?

In ASP.NET MVC WebAPI project by default we have created following controller
public class ValuesController : ApiController
{
// 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)
{
}
}
But is possible to add here any custom methods so they can support get/post as well?
Thank you!
You can use attributes such as the RoutePrefix with the Http type.
[Route("ChangePassword")]
[HttpPost] // There are HttpGet, HttpPost, HttpPut, HttpDelete.
public async Task<IHttpActionResult> ChangePassword(ChangePasswordModel model)
{
}
The http type will map it back to its correct method in combination with the Route name.
I am not sure I follow as you have GET and POST right there in your code, but in any case you have other options:
Option 1
First, you can configure your custom Routes in the App_Start folder in the WebApiConfig.cs file. Here is what I normally use:
// GET /api/{resource}/{action}
config.Routes.MapHttpRoute(
name: "Web API RPC",
routeTemplate: "{controller}/{action}",
defaults: new { },
constraints: new { action = #"[A-Za-z]+", httpMethod = new HttpMethodConstraint("GET") }
);
// GET|PUT|DELETE /api/{resource}/{id}/{code}
config.Routes.MapHttpRoute(
name: "Web API Resource",
routeTemplate: "{controller}/{id}/{code}",
defaults: new { code = RouteParameter.Optional },
constraints: new { id = #"\d+" }
);
// GET /api/{resource}
config.Routes.MapHttpRoute(
name: "Web API Get All",
routeTemplate: "{controller}",
defaults: new { action = "Get" },
constraints: new { httpMethod = new HttpMethodConstraint("GET") }
);
// PUT /api/{resource}
config.Routes.MapHttpRoute(
name: "Web API Update",
routeTemplate: "{controller}",
defaults: new { action = "Put" },
constraints: new { httpMethod = new HttpMethodConstraint("PUT") }
);
// POST /api/{resource}
config.Routes.MapHttpRoute(
name: "Web API Post",
routeTemplate: "{controller}",
defaults: new { action = "Post" },
constraints: new { httpMethod = new HttpMethodConstraint("POST") }
);
// POST /api/{resource}/{action}
config.Routes.MapHttpRoute(
name: "Web API RPC Post",
routeTemplate: "{controller}/{action}",
defaults: new { },
constraints: new { action = #"[A-Za-z]+", httpMethod = new HttpMethodConstraint("POST") }
);
I use a combination of RESTful endpoints as well as RPC endpoints. For some purists, this is grounds for a holy war. For me, I use a combination of the two because it is a powerful combination and I can't find any sane reason not to.
Option 2
As the others have pointed out and as I myself am doing more of these days, use attribute routing:
[HttpGet]
[GET("SomeController/SomeUrlSegment/{someParameter}")]
public int SomeUrlSegment(string someParameter)
{
//do stuff
}
I needed a NuGet package for attribute routing to make this work (just search NuGet for "Attribute Routing"), but I think that MVC 5/WebAPI 2 has it natively.
Hope this helps.
You could use attribute routing:
[Route("customers/{customerId}/orders")]
public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }
Some documentation to get you started:
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
First Put this route to webapiconfig.cs
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Now you can add actions to your controllers like this
[HttpPost]
public void Upload()
{
//Do some thing
}
I decorated upload action with httppost attribute that means this action accept just only post requests , if you want to actions to be GET , you can remove attribute or just decorate to your suite

Multiple actions were found that match the request Web API?

I am using web API and i am new in this. I am stuck in a routing problem. I have a controller with following actions :
// GET api/Ceremony
public IEnumerable<Ceremony> GetCeremonies()
{
return db.Ceremonies.AsEnumerable();
}
// GET api/Ceremony/5
public Ceremony GetCeremony(int id)
{
Ceremony ceremony = db.Ceremonies.Find(id);
return ceremony;
}
public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
{
return filter.Ceremonies();
}
The problem occure when i added the action GetFilteredCeremonies to my controller. After adding this when i make an ajax call to GetCeremonies action then it return an Exception with following message :
"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were
found that match the request
FYI: The parameter Search is the Model class which contains properties and a function name Ceremonies.
EDIT
Route:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
If you're not requirement bound to use REST services that use api/{controller}/{id} route and attempt to resolve action based on method GET/POST/DELETE/PUT, you can modify your route to classic MVC route to api/{controller}/{action}/{id} and it will solve your problems.
The problem here is your 2 Get methods will resolve to api/Ceremony and MVC does not allow parameter overloading. A quick workaround (not necessarily the preferred approach) for this sort of problem is to make your id parameter nullable e.g.
// GET api/Ceremony
public IEnumerable<Ceremony> GetCeremonies(int? id)
{
if (id.HasValue)
{
Ceremony ceremony = db.Ceremonies.Find(id);
return ceremony;
}
else
{
return db.Ceremonies.AsEnumerable();
}
}
However, you would then be returning a list of ceremonies when with 1 item when your trying to query for a single ceremony - if you could live with that then it may be the solution for you.
The recommended solution is to map your paths appropriately to the correct actions e.g.
context.Routes.MapHttpRoute(
name: "GetAllCeremonies",
routeTemplate: "api/{controller}",
defaults: new { action = "GetCeremonies" }
);
context.Routes.MapHttpRoute(
name: "GetSingleCeremony",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GetCeremony", id = UrlParameter.Optional }
);
Luckily nowadays with WEB API2 you can use Attribute Routing. Microsoft has gone open source on a big scale and then a wizard named Tim McCall contributed it from the community. So since somewhere end 2013, early 2014 you can add attributes like [Route("myroute")] on your WEB API methods. See below code example.
Still - as I just found out - you have to make sure to use System.Web.Http.Route and NOT System.Web.Mvc.Route. Otherwise you'll still get the error message Multiple actions were found that match the request.
using System.Web.Http;
...
[Route("getceremonies")]
[HttpGet]
// GET api/Ceremony
public IEnumerable<Ceremony> GetCeremonies()
{
return db.Ceremonies.AsEnumerable();
}
[Route("getceremony")]
[HttpGet]
// GET api/Ceremony/5
public Ceremony GetCeremony(int id)
{
Ceremony ceremony = db.Ceremonies.Find(id);
return ceremony;
}
[Route("getfilteredceremonies")]
[HttpGet]
public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
{
return filter.Ceremonies();
}
Here is my controller:
public class PhoneFeaturesController : ApiController
{
public List<PhoneFeature> GetbyPhoneId(int id)
{
var repository = new PhoneFeatureRepository();
return repository.GetFeaturesByPhoneId(id);
}
public PhoneFeature GetByFeatureId(int id)
{
var repository = new PhoneFeatureRepository();
return repository.GetFeaturesById(id);
}
}
Here is my api routing:
config.Routes.MapHttpRoute(
name: "ApiWithId",
routeTemplate: "Api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = #"^[0-9]+$" });
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null,
constraints: new { name = #"^[a-z]+$" });
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "Get" },
constraints: new { id = #"^[0-9]+$" });
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I tested it like this:
/api/PhoneFeatures//api/PhoneFeatures/GetFeatureById/101
/api/PhoneFeatures/GetByFeatureId/12
It works smooth in every condition :)
I found another fix that doesn't require moving methods out of the controller or changing the route mapping config. Just add the [NonAction] attribute to the method you want to exclude:
[NonAction]
public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
Please check you have two methods which has the different name and same parameters.
If so please delete any of the method and try.
This error was raised because there are two methods which are looking for same parameters. try to delete any one of them and try...
I hope you are doing HttpGet while you invoke GetFilteredCeremonies(Search filter)
In that case, you cannot pass complex object in GET request like Search that you are passing.
If for some reason, you definitely want to get complex types in your get request, there are some work around. You may need to write a custom model binder and then set the attribute. please refer this article.
Edit Your WebApiConfig.cs in App_Start folder on the root of project and add {action} to routeTemplate parameter in MapHttpRoute Method like below :
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

Categories