asp.net-web-api-routing

Web API 2 Is it possible to load a route/controller programmatically?

China☆狼群 提交于 2019-12-03 14:05:58
I am currently working on an enterprise web application that uses WCF to implement a REST API. It utilizes a VirtualPathProvider to catch requests to *.svc files (which don't actually exist), and then builds them on the fly to dynamically load the associated WCF services. This allows the system to have "modules" that can be added to the application at runtime without impacting the web server or anyone using it. What I would like to know, is if the same is conceptually possible with Web API 2. I've been doing some research, but it looks like the routes can only be configured at startup... What

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

会有一股神秘感。 提交于 2019-12-03 13:22:43
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 in this manner? The behavior you are noticing is more related to Action selection & Model binding

Web API Routes to support both GUID and integer IDs

风格不统一 提交于 2019-12-03 11:06:20
问题 How can I support GET routes for both GUID and integer? I realize GUIDs are not ideal, but it is what it is for now. I'm wanting to add support for integers to make it easier for users to remember and communicate what should be unique "keys." Example routes: testcases/9D9A691A-AE95-45A4-A423-08DD1A69D0D1 testcases/1234 My WebApiConfig : public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); var routes = config.Routes; routes.MapHttpRoute("DefaultApiWithAction

How to pass/receive multiple args to a RESTful Web API GET method?

早过忘川 提交于 2019-12-03 09:39:00
问题 The usual examples of GET RESTful methods that take a parameter (returning a scalar value rather than a dataset) are shown like so: public string Get(int id) { //get and return the value } ...where the val passed is typically an ID, so you can use it to get a scalar value based on that unique value. What, though, if you want to pass multiple values, such as a string and an int? Is it simply a matter of defining a method like so: public string Get(string someString, int someInt) { //get and

UriPathExtensionMapping to control response format in WebAPI

和自甴很熟 提交于 2019-12-03 08:52:09
问题 I'm having a problem getting UriPathExtensionMapping working in ASP.NET WebAPI. My setup is as follows: My routes are: config.Routes.MapHttpRoute( name: "Api UriPathExtension", routeTemplate: "api/{controller}.{extension}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "Api UriPathExtension ID", routeTemplate: "api/{controller}/{id}.{extension}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "DefaultApi",

$Metadata with WebAPi OData Attribute Routing Not Working

落花浮王杯 提交于 2019-12-03 08:46:07
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().SingleOrDefault(x => x.Id== key); if (result == null) return NotFound(); return Ok(result); } } Here is

Multiple actions for the same HttpVerb

南楼画角 提交于 2019-12-03 07:26:37
I have a Web API controller with the following actions: [HttpPut] public string Put(int id, JObject data) [HttpPut, ActionName("Lock")] public bool Lock(int id) [HttpPut, ActionName("Unlock")] public bool Unlock(int id) And the following routes mapped: routes.MapHttpRoute( name: "Api", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); routes.MapHttpRoute( name: "ApiAction", routeTemplate: "api/{controller}/{action}/{id}" ); When I make the following requests everything works as expected: PUT /api/items/Lock/5 PUT /api/items/Unlock/5 But when I attempt to

Modular ASP.NET Web API: how to add/remove route at run time to a Web API

那年仲夏 提交于 2019-12-03 07:13:06
I am trying to design a modular Web API application (It is not an MVC app!) in which a user in admin role can add or remove modules without restarting the ASP.NET application. Module: each module is an assembly (.dll file) that contains at least one class which is derived from ApiController . Routing is based on Attribute Routing in ASP.NET Web API 2 Production of modules (assemblies) is not in the scope of this question. Modules (assembly files) are copied to / deleted from `~/plugins/ folder in the root of the project. This process is not in the scope of this question either. The main ASP

How can I generate a WebApi2 URL without specifying a Name on the Route attribute with AttributeRouting?

巧了我就是萌 提交于 2019-12-03 04:43:48
I've configured my ASP.NET MVC5 application to use AttributeRouting for WebApi: public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); } } I have an ApiController as follows: [RoutePrefix("api/v1/subjects")] public class SubjectsController : ApiController { [Route("search")] [HttpPost] public SearchResultsViewModel Search(SearchCriteriaViewModel criteria) { //... } } I would like to generate a URL to my WebApi controller action without having to specify an explicit route name. According to this page on CodePlex , all MVC

Web API Routes to support both GUID and integer IDs

≯℡__Kan透↙ 提交于 2019-12-03 01:29:42
How can I support GET routes for both GUID and integer? I realize GUIDs are not ideal, but it is what it is for now. I'm wanting to add support for integers to make it easier for users to remember and communicate what should be unique "keys." Example routes: testcases/9D9A691A-AE95-45A4-A423-08DD1A69D0D1 testcases/1234 My WebApiConfig : public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); var routes = config.Routes; routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}"); routes.MapHttpRoute("DefaultApiWithKey", "Api/{controller}/{key}", new