asp.net-web-api-routing

+ (plus) sign in Web API routing

↘锁芯ラ 提交于 2019-12-20 10:27:47
问题 I'm working with an asp.net web api project, and I have to pass an mobile number through a post. But i cannot return a plus sign. my route: config.Routes.MapHttpRoute( name: "SmsRoute", routeTemplate: "rest/sms/{country}/{company}/phone/{mobilenumber}", defaults: new { controller = "Sms", action = "PostSms" }); controller: public HttpResponseMessage PostSms(string country, string company, string mobilenumber) { return Request.CreateResponse( HttpStatusCode.Created ); } When I run this post

Web API routing with multiple parameters

隐身守侯 提交于 2019-12-20 09:34:35
问题 I'm trying to work out how to do the routing for the following Web API controller: public class MyController : ApiController { // POST api/MyController/GetAllRows/userName/tableName [HttpPost] public List<MyRows> GetAllRows(string userName, string tableName) { ... } // POST api/MyController/GetRowsOfType/userName/tableName/rowType [HttpPost] public List<MyRows> GetRowsOfType(string userName, string tableName, string rowType) { ... } } At the moment, I'm using this routing for the URLs: routes

How can I route an External Class Library in my Web API, ASP NET 5

时光毁灭记忆、已成空白 提交于 2019-12-20 07:48:59
问题 I have created an WEB API in ASP NET 5 and I can reference an external Class Library vNext. I am working on Visual Studio 2015 Community. In that Library I have this controller: [Route("api/[controller]")] public class NovosController : Controller { // GET: api/values [HttpGet] public IEnumerable<string> Get() { return new string[] { "i am", "an external library" }; } To add this library in my Web API´s references I had to browse the DLL's (one for dnxcore50 and one for dnx451). This is the

WebApi 2.0 Routes not matching with query parameters?

廉价感情. 提交于 2019-12-19 03:45:38
问题 I've just switched from AttributeRouting to WebApi 2.0 AttributeRouting, and have got a controller and action defined like so: public class InvitesController : ApiController { [Route("~/api/invites/{email}")] [HttpGet] [ResponseType(typeof(string))] public IHttpActionResult InviteByEmail(string email) { return this.Ok(string.Empty); } } Example query: GET: http://localhost/api/invites/test@foo.com The response I receive is a 200, with empty content (due to string.Empty). This all works fine -

WebAPI 2 attribute routing enable session state

♀尐吖头ヾ 提交于 2019-12-18 15:44:10
问题 We figured out how to enable session state with webapi Sample here Now we have WebApi 2 attribute routing, so we no longer have route object to inject custom handler. Is there any way of enabling session state with attribute routing? 回答1: You need to add this to global.asax protected void Application_PostAuthorizeRequest() { System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required); } Then you could access the session through: HttpContext

How to map WebAPI routes correctly

可紊 提交于 2019-12-18 10:59:19
问题 I'm building an API for a Twitter like site using Web API and have trouble with mapping the routes I have the following actions for the User controller: public User Get(string firstname, string lastname) public User Get(Guid id) public User Friends(Guid id) public User Followers(Guid id) public User Favorites(Guid id) The desired routes and the generated documentation should be: api/users?firstname={firstname}&lastname={lastname} api/users/{id} api/users/{id}/friends api/users/{id}/followers

Query parameter route constraints

纵然是瞬间 提交于 2019-12-18 05:11:12
问题 I just started using ASP.NET Web API 2.1 and have encountered a limitation. Using Attribute Routing, I can do the following: [Route("item/{id:int}")] public IHttpActionResult GetItem(int id) { ... } The URL /item/5 will be routed to this action, but the URL /item/abc will not, because of the int constraint in {id:int} . I tried changing my URL so that the id parameter was in the query string along with its constraint, despite the use of route constraints on query parameters never being

AttributeRouting not working with HttpConfiguration object for writing Integration tests

最后都变了- 提交于 2019-12-18 04:18:05
问题 I'm creating some integration tests following the ideas outlined here: http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/ When I try to register routes from a hand crafted HttpConfiguration object I'm getting the following error: "The constraint entry 'inboundHttpMethod' on the route with route template 'api/Contacts/{id}' must have a string value or be of a type which implements 'IHttpRouteConstraint'." Sample code: Controller: [RoutePrefix("api")]

Ambiguous Controller Names with Routing attributes: controllers with same name and different namespace for versioning

牧云@^-^@ 提交于 2019-12-18 02:44:32
问题 I am trying to add API versioning and my plan is to create a controller for each version in different namespace. My project structure looks like this (note: no separate area for each version) Controllers | |---Version0 | | | |----- ProjectController.cs | |----- HomeController.cs | |---Version1 | |----- ProjectController.cs |----- HomeController.cs I am using RoutingAttribute for the routes. So, ProjectController in Version0 has function with route as namespace MyProject.Controllers.Version0 {

CreatedAtRoute routing to different controller

狂风中的少年 提交于 2019-12-17 23:05:01
问题 I'm creating a new webapi using attribute routing to create a nested route as so: // PUT: api/Channels/5/Messages [ResponseType(typeof(void))] [Route("api/channels/{id}/messages")] public async Task<IHttpActionResult> PostChannelMessage(int id, Message message) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != message.ChannelId) { return BadRequest(); } db.Messages.Add(message); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = message.Id },