asp.net-web-api-routing

Web API route to action name

蓝咒 提交于 2019-11-30 06:38:22
问题 I need a controller to return JSON to be consumed by JavaScript so I inherited from the ApiController class but it isn't behaving as I expected. The Apress book Pro ASP.NET MVC 4 and most of the online examples I've found give examples like: public class ServicesController : ApiController { public string[] MethodFruit() { return new string[] { "Apple", "Orange", "Banana" }; } accessed via the URL: http://mysite/services/methodfruit But that never works - the resource isn't found. The only

How to map WebAPI routes correctly

*爱你&永不变心* 提交于 2019-11-30 00:30:20
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 api/users/{id}/favorites In WebApiConfig.cs I have: config.Routes.MapHttpRoute( "2", "api/{controller}/

No type was found that matches the controller named 'User'

╄→гoц情女王★ 提交于 2019-11-30 00:19:06
I'm trying to navigate to a page which its URL is in the following format: localhost:xxxxx/User/{id}/VerifyEmail?secretKey=xxxxxxxxxxxxxxx I've added a new route in the RouteConfig.cs file and so my RouteConfig.cs looks like this: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "VerifyEmail", url: "User/{id}/VerifyEmail", defaults: new { controller = "User", action = "VerifyEmail" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller =

Route to different actions based on json value

£可爱£侵袭症+ 提交于 2019-11-29 23:26:40
问题 I would like to route requests to different actions based on the value of a particular json parameter. For example, given the following json data: { type: "type1", type1data: "type1value" } and { type: "type2", type2data: "type2value" } I'd like to be able to have 2 different actions on my ApiController: void CreateType1(string type1data) { // ... } void CreateType2(string type2data) { //... } How can something like this be done? Update: I'd like the same URL if possible. Something like

CreatedAtRoute routing to different controller

非 Y 不嫁゛ 提交于 2019-11-29 22:53:09
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 }, message); } I however want to return a route that isn't nested i.e.: /api/Messages/{id} which is defined

WebApi: mapping parameter to header value

两盒软妹~` 提交于 2019-11-29 22:36:37
I've done a few searches but haven't seem to find anything... Using WebApi, I would like to map an input parameter to a header value: e.g. E.g. in controller: public User GetUser(int id){ ... return user; } I want WebApi to map the id parameter to a header value (e.g. X-Auth: 1234)... rather than an URL parameter. Is this supported? I don't think this is supported out of the box, like for example with the [FromBody] attribute. It seems you should be able to achieve this functionality by using Model Binders, as described here . In the model binder you have access to the request and its headers,

asp.net webapi 2 attribute routing not working

守給你的承諾、 提交于 2019-11-29 20:55:58
I have visual studio 2012 installed with mvc4 using .net framework 4.5. Now I want to use webapi2 with attribute writing and i want my hlep page show all the endpoints properly. In my solution i added a new mvc4 base emtpy project and using nuget i upgraded to mvc5 and then i have installed webapi2 packages. lastly i have installed help package for webapi2. now when i use routeprefix I cant see any content on help page and when i try to access my webapi endpoint in browsers it throws following error. http://expressiis.com/api/v1/ <Error> <Message> No HTTP resource was found that matches the

Multiple controllers with same URL routes but different HTTP methods

房东的猫 提交于 2019-11-29 19:30:30
问题 I've got a following two controllers: [RoutePrefix("/some-resources") class CreationController : ApiController { [HttpPost, Route] public ... CreateResource(CreateData input) { // ... } } [RoutePrefix("/some-resources") class DisplayController : ApiController { [HttpGet, Route] public ... ListAllResources() { // ... } [HttpGet, Route("{publicKey:guid}"] public ... ShowSingleResource(Guid publicKey) { // ... } } All three actions got in fact three different routes: GET /some-resources POST

Web Api 2.2 OData V4 Function Routing

♀尐吖头ヾ 提交于 2019-11-29 17:26:49
问题 I have a Web Api 2.2 project working with OData v4. The normal EntitySet configuration is working as desired with all http verbs. Where I am having a problem is trying to expose a custom function. I started off trying to do something different than the standard examples, but I have backed all the way up to just trying to getting a basic example function working. Here is my startup config (straight from the MS examples): using System; using System.Collections.Generic; using System.Linq; using

How can I tell the Web API / Castle Windsor routing engine to use a different database instance in my Repository?

笑着哭i 提交于 2019-11-29 17:25:11
My understanding of the flow of events with an ASP.NET Web API Castle Windsorized app that uses Models, Repositories, and Controllers: 0) The client calls a REST method via a URI such as: http://localhost:28642/api/platypi/Count 1) Castle Windsor's routing engine maps intercepts that incoming call, sending the registered concrete class that implements the interface platypiController has as an arg in its constructor. 2) That constructor determines which of its methods is to be called (what corresponds to "Count" in this case). 3) That Controller method calls a corresponding method on the