asp.net-web-api-routing

MVC API Routing When Multiple Get Actions Are Present

风格不统一 提交于 2019-12-05 01:30:29
问题 There seems to be a thousand people asking the same question on stack overflow, but there doesn't seem to be a single solution to this problem. I am going to ask it again... I have an API controller that has the following actions: // GET api/Exploitation public HttpResponseMessage Get() { var items = _exploitationRepository.FindAll(); var mappedItems = Mapper.Map<IEnumerable<Exploitation>, IEnumerable<ExploitationView>>(items); var response = Request.CreateResponse<IEnumerable

MVC Routes within WebAPI controller

眉间皱痕 提交于 2019-12-05 00:49:09
问题 Quick question regarding routes within MVC and WebAPI. I have added a route to route config.cs: routes.MapRoute( name: "ConfirmEmail", url: "ConfirmEmail/{userid}", defaults: new { controller = "Email", action = "ConfirmEmail" } ); This is registered in the global.asax as per normal: RouteConfig.RegisterRoutes(RouteTable.Routes); I am trying to generate a URL for use within an email which is sent as part of a function call within a WebAPI controller function. I am using the UrlHelper.Link

The 'DelegatingHandler' list is invalid because the property 'InnerHandler' of 'handler' is not null

自古美人都是妖i 提交于 2019-12-04 16:02:08
I have a custom message handler that I add globally as follows: public class CustomerHandler : DelegatingHandler { public CustomHandler(HttpConfiguration httpConfiguration) { InnerHandler = new HttpControllerDispatcher(httpConfiguration); } } config.MessageHandlers.Add(new CustomHandler(config)); However, I get the following error: The 'DelegatingHandler' list is invalid because the property 'InnerHandler' of 'CustomHandler' is not null. Parametername: handlers So I changed the code to: config.MessageHandlers.Add(new CustomHandler(config) { InnerHandler = null }); And I get an error:

ASP.NET Web API multiple RoutePrefix

为君一笑 提交于 2019-12-04 15:59:59
问题 The opensource Attribute Routing allows to have multiple route-prefixes. Why does ASP.NET Web API 2.0 does not allow to have multiple RoutePrefix(). [RoutePrefix("api/v1/{abc}/Entity")] [RoutePrefix("api/v1/{abc}/{xyz?}/Entity")] public class MyApiController : ApiController { [Route("")] public IHttpResult Get() { return Ok("Hello World"); } } 回答1: You can add a route to the action method also overriding the RoutePrefix with a "~" example: [RoutePrefix("api/v1/{abc}/Entity")] public class

$Metadata with WebAPi OData Attribute Routing Not Working

天大地大妈咪最大 提交于 2019-12-04 14:03:02
问题 I'm using OData Attribute Routing for an OData endpoint. Here is an example of what I have: [ODataRoutePrefix("Profile")] public class ProfileODataController : ODataController { [ODataRoute] [EnableQuery] public IHttpActionResult Get() { var repo = new Repositories.ProfileRepository(); return Ok(repo.GetProfiles()); } [ODataRoute("({key})")] [EnableQuery] public IHttpActionResult Get([FromODataUri] string key) { var repo = new Repositories.ProfileRepository(); var result = repo.GetProfiles()

The Constraint Entry 'httpMethod' on the Route Must Have a String Value

两盒软妹~` 提交于 2019-12-04 07:42:06
I have an asp.net Web API project, and in my WebApiConfig file, I have the following route defined: config.Routes.MapHttpRoute( name: "Web API Get", routeTemplate: "api/{controller}", defaults: new { action = "Get" }, constraints: new { httpMethod = new HttpMethodConstraint("GET") } ); For integration testing purposes, I want to make a request to an HttpSelfHostServer to verify that we are receiving the proper data back from the api call. I am making the HttpRequestMessage as follows: var httpMethod = new HttpMethod("GET"); var request = new HttpRequestMessage(httpMethod, "http://localhost

HttpClient on WebApi is extremely slow

不打扰是莪最后的温柔 提交于 2019-12-04 04:46:39
问题 I'm implementing a Proxy for my application using ASP.NET WebApi (ApiController) and using HttpClient to make the request with my authorization header. It works fine, but it's extremely slow. Below is the main code, then the Global initialization (with DefaultConnectionLimit) and web.config related piece. As you can see, I'm already using a static/shared HttpClient object with no Proxy and HttpCompletionOption.ResponseHeadersRead on the actual request. This WebApi endpoint is called in

Generic Web Api method

喜欢而已 提交于 2019-12-04 04:30:36
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 web api method like [HttpPost] void Post<T>(T model) where T : ModelBase If I simply create a generic

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

℡╲_俬逩灬. 提交于 2019-12-04 03:07:53
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 , where products is not defined in this "partial" API route. We want to still route /api/products to

Route all Web API requests to one controller method

跟風遠走 提交于 2019-12-04 00:54:24
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? 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 this dead simple implementation. All you have to do is setup a wildcard route. Example: public class