asp.net-web-api-routing

Accessing ApiController Type in DelegatingHandler

不想你离开。 提交于 2019-12-21 17:17:09
问题 I am using Asp.Net WebAPI for a project. I am currently working on authentication and authorization. I have a messageHandler that will check the HTTP authentication header of a request and build my identity and user profile. However, I want to annotate my controller action (or just the controller) with claims that the action may require (we have a lot of claims that a user can have, so I don't want to load them all). e.g.: public class MyController : ApiController { [LoadClaims("SomeClaim",

Generic Web Api method

南笙酒味 提交于 2019-12-21 12:10:47
问题 I've some classes like CustomerModel or CustomerDetailsModel which are inherting from ModelBase . Also i don't want to introduce subclasses for each modeltype. In one case have a post method foreach. So i could manually create multiple routes to call a method which looks like Handle<T>(T model) where T : ModelBase They only differ in the path the were called. For example: baseurl/api/customer => CustomerModel baseurl/api/customerdetails => CustomerDetailsModel I want to implement a generic

Expose only a subset of .NET OData APIs for a route (return 404 for excluded APIs)

我的未来我决定 提交于 2019-12-21 09:15:35
问题 Background/Context: We have two routes, with different route prefixes: Route 1 prefix: /api Route 2 prefix: /api/partial Currently, we use the same EdmModel for both route prefixes. (See the first code snippit, named "What we currently do"). What we want: We need to only allow a subset of API functionality for Route 2: /api/partial . We want to return 404 when someone tries to access an API that is not available to the "partial" EdmModel Example: We want to return 404 for /api/parial/products

Route all Web API requests to one controller method

吃可爱长大的小学妹 提交于 2019-12-21 07:34:17
问题 Is it possible to customize ASP.NET Web API's routing mechanism to route all requests to the API to one controller method? If a request comes in to www.mysite.com/api/products/ or www.mysite.com/api/otherResource/7 All would be routed to my SuperDuperController's Get() method? 回答1: I ran into a case where I needed to do this. (Web API 2) I first looked into creating custom IHttpControllerSelector and IHttpActionSelector s. However, that was a bit of a murky way around. So I finally settled on

How to force web API to recognise querystring parameter

风格不统一 提交于 2019-12-21 07:20:33
问题 ASP.NET MVC4 Web API v1 controller is defined below. It should accept 1 or 2 query string parametrs. However ko parameter is always null if method is called. Request is below. How to fix so that klient or namepart parameter can passed in query string ? Web API v1 controller: namespace MyApp.Controllers { public class CustomersSearchViewModel { public string Klient { get; set; } public string Namepart { get; set; } } [Authorize] public class CustomersController : ApiController { public

How to use Route attribute to bind query string with web API?

喜夏-厌秋 提交于 2019-12-21 05:38:14
问题 I'm trying to get this to work: [Route("api/Default")] public class DefaultController : ApiController { [HttpGet, Route("{name}")] public string Get(string name) { return $"Hello " + name; } } by calling this http://localhost:55539/api/Default?name=rami but not working, tried this also: http://localhost:55539/api/Default/Hello?name=rami , Also this not working: http://localhost:55539/api/Default/Hello/rami 回答1: Make sure attribute routing is enabled in WebApiConfig.cs config

How to send an array via a URI using Attribute Routing in Web API?

删除回忆录丶 提交于 2019-12-21 04:04:34
问题 I'm following the article on Attribute Routing in Web API 2 to try to send an array via URI: [HttpPost("api/set/copy/{ids}")] public HttpResponseMessage CopySet([FromUri]int[] ids) This was working when using convention-based routing: http://localhost:24144/api/set/copy/?ids=1&ids=2&ids=3 But with attribute routing it is no longer working - I get 404 not found. If I try this: http://localhost:24144/api/set/copy/1 Then it works - I get an array with one element. How do I use attribute routing

Web API 2 / MVC 5 : Attribute Routing passing parameters as querystring to target different actions on same controller

十年热恋 提交于 2019-12-20 19:42:48
问题 I've been playing with the new Web API 2 (which looks very promising btw) but I'm having a bit of a headache to get some routes working. All works fine when I have GetAllUsers / GetUser(int id), but then when I add GetUserByName(string name) and/or GetUserByUsername(string username) things start to be creepy. I know that the int will be the first one and that I can re-order the routes but let's imagine the following scenario: A user can have a valid username=1234 or name=1234 (I know it's

Multiple Routes on a Controller

情到浓时终转凉″ 提交于 2019-12-20 11:21:06
问题 Was wondering if it was possible to have more than one route pointing to a WebApi controller? For example I will like to have both http://domain/calculate and http://domain/v2/calculate pointing to the same controller function? 回答1: public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // Configure Web API to use only bearer token authentication. config.SuppressDefaultHostAuthentication(); config.Filters.Add(new

Multiple Routes on a Controller

此生再无相见时 提交于 2019-12-20 11:19:08
问题 Was wondering if it was possible to have more than one route pointing to a WebApi controller? For example I will like to have both http://domain/calculate and http://domain/v2/calculate pointing to the same controller function? 回答1: public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // Configure Web API to use only bearer token authentication. config.SuppressDefaultHostAuthentication(); config.Filters.Add(new