asp.net-web-api-routing

Optional Parameters in Web Api Attribute Routing

社会主义新天地 提交于 2019-11-27 08:32:39
I want to handle POST of the following API-Call: /v1/location/deviceid/appid Additional Parameter are coming from the Post-Body. This all works fine for me. Now I wnat to extend my code by allowing "deviceid" and/or "appid" and/or BodyData to be null: /v1/location/deviceid /v1/location/appid /v1/location/ These 3 URLs should responded by the same route. My first approach (BodyData required): [Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")] public location_fromuser Post(string deviceid = null, string appid = null, [FromBody] location_fromuser BodyData) { return repository

Query string not working while using attribute routing

依然范特西╮ 提交于 2019-11-27 06:29:11
I'm using System.Web.Http.RouteAttribute and System.Web.Http.RoutePrefixAttribute to enable cleaner URLs for my Web API 2 application. For most of my requests, I can use routing (eg. Controller/param1/param2 ) or I can use query strings (eg. Controller?param1=bob&param2=mary ). Unfortunately, with one of my Controllers (and only one), this fails. Here is my Controller: [RoutePrefix("1/Names")] public class NamesController : ApiController { [HttpGet] [Route("{name}/{sport}/{drink}")] public List<int> Get(string name, string sport, string drink) { // Code removed... } [HttpGet] [Route("{name}/

Is there a default verb applied to a Web API ApiController method?

不问归期 提交于 2019-11-27 05:04:14
I've been looking at the code (in https://github.com/patelsan/WebAPIAuthentication ) from this article: http://www.codeproject.com/Articles/630986/Cross-Platform-Authentication-With-ASP-NET-Web-API . It's pretty good and seems to work fine. There are very few articles that explain this kind of token authentication, but this is the best I've seen. Note that I'm new to this technology and there's much to learn. So, I noticed that the UsersController has this code: public class UsersController : ApiController { public Status Authenticate(User user) { . . . } } The Authenticate method doesn't

Multiple Controller Types with same Route prefix ASP.NET Web Api

点点圈 提交于 2019-11-27 04:05:57
Is it possible to separate GETs and POSTs into separate API Controller types and accessing them using the same Route Prefix? Here are my controllers: [RoutePrefix("api/Books")] public class BooksWriteController : EventStoreApiController { [Route("")] public void Post([FromBody] CommandWrapper commandWrapper){...} } [RoutePrefix("api/Books")] public class BooksReadController : MongoDbApiController { [Route("")] public Book[] Get() {...} [Route("{id:int}")] public Book Get(int id) {...} } Web API (1.x-2.x) does not support multiple attribute routes with the same path on different controllers.

Swashbuckle set manualy operationId, mutiple opertaions with same verb

早过忘川 提交于 2019-11-27 03:20:14
问题 I need to know if it's possible to set up custom operationid, or a naming convention, I mean I know that operation filter can be overwritten the way how operationId is generated https://azure.microsoft.com/en-us/documentation/articles/app-service-api-dotnet-swashbuckle-customize/ using Swashbuckle.Swagger; using System.Web.Http.Description; namespace Something { public class MultipleOperationsWithSameVerbFilter : IOperationFilter { public void Apply( Operation operation, SchemaRegistry

Routing based on query string parameter name

廉价感情. 提交于 2019-11-27 01:04:49
问题 I'm trying to configure routing in my MVC4 WebAPI project. I want to be able to search for products based on their name or their type like so: /api/products?name=WidgetX - returns all products named WidgetX /api/products?type=gadget - returns all products of type gadget The routes are configured like this: config.Routes.MapHttpRoute( name: "Get by name", routeTemplate: "api/products/{name}", defaults: new { controller = "ProductSearchApi", action = "GetProductsByName", name = string.Empty } )

Optional Parameters in Web Api Attribute Routing

余生长醉 提交于 2019-11-26 22:17:22
问题 I want to handle POST of the following API-Call: /v1/location/deviceid/appid Additional Parameter are coming from the Post-Body. This all works fine for me. Now I wnat to extend my code by allowing "deviceid" and/or "appid" and/or BodyData to be null: /v1/location/deviceid /v1/location/appid /v1/location/ These 3 URLs should responded by the same route. My first approach (BodyData required): [Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")] public location_fromuser Post

Routing with multiple Get methods in ASP.NET Web API

假如想象 提交于 2019-11-26 19:22:47
I am using Web Api with ASP.NET MVC, and I am very new to it. I have gone through some demo on asp.net website and I am trying to do the following. I have 4 get methods, with the following signatures public List<Customer> Get() { // gets all customer } public List<Customer> GetCustomerByCurrentMonth() { // gets some customer on some logic } public Customer GetCustomerById(string id) { // gets a single customer using id } public Customer GetCustomerByUsername(string username) { // gets a single customer using username } For all the methods above I would like to have my web api somewhat like as

Attribute routing and inheritance

不打扰是莪最后的温柔 提交于 2019-11-26 16:14:27
I am playing around with the idea of having a base controller that uses a generic repository to provide the basic CRUD methods for my API controllers so that I don't have to duplicate the same basic code in each new controller. But am running into problems with the routing attribute being recognized when it's in the base controller. To show exactly what the problem I'm having I've created a really simple WebAPI controller. When I have a Get method in the main Controller and it inherits from the ApiController directly I don't have any problems and this works as expected. [RoutePrefix("admin

Custom method names in ASP.NET Web API

寵の児 提交于 2019-11-26 14:57:20
I'm converting from the WCF Web API to the new ASP.NET MVC 4 Web API. I have a UsersController, and I want to have a method named Authenticate. I see examples of how to do GetAll, GetOne, Post, and Delete, however what if I want to add extra methods into these services? For instance, my UsersService should have a method called Authenticate where they pass in a username and password, however it doesn't work. public class UsersController : BaseApiController { public string GetAll() { return "getall!"; } public string Get(int id) { return "get 1! " + id; } public User GetAuthenticate(string