asp.net-web-api-routing

Route attribute routing with query strings when there are multiple routes

北城余情 提交于 2020-02-13 22:24:12
问题 I have this: [HttpGet] [Route("Cats")] public IHttpActionResult GetByCatId(int catId) [HttpGet] [Route("Cats")] public IHttpActionResult GetByName(string name) They are called by providing the query string eg Cats?catId=5 However MVC Web API will say you can't have multiple routes that are the same (both routes are "Cats". How can I get this to work so MVC Web API will recognize them as separate routes? Is there something I can put into the Route property? It says that ? is an invalid

asp.net mvc web api help page not populating for given route

柔情痞子 提交于 2020-01-26 01:41:37
问题 I am having difficulties having the help page populate for a web api controller. The only method in the controller is the following: public HttpResponseMessage Get(string p1,string p2= "blend", string p3 = "blend") The method appears reflected in the help page with the signature: GET api/mycontroller?p1={p1}&p2={p2}&p3={p3} However none of my comments flow to the help page. For other simple controller Get(string id) the help page works ok. I tried adding the following on the WebApiConfig.cs

need route for my web api 2 controller

落爺英雄遲暮 提交于 2020-01-24 08:48:28
问题 I have a simple WebApi2 controller that returns XML but I cannot add another method properly with the routing I have defined: namespace CBMI.WebAPIservice.Controllers { public class MarkersController : ApiController { public HttpResponseMessage Get(int? id) { int i = id.HasValue ? id.Value : 0; XmlDocument docContent = GetXmlDataFromDB(i); return new HttpResponseMessage { Content = new StringContent(docContent.InnerXml.ToString(), Encoding.UTF8, "application/xml") }; } public

An API version is required, but was not specified. webapi

99封情书 提交于 2020-01-23 17:01:52
问题 var constraintResolver = new DefaultInlineConstraintResolver() { ConstraintMap = { ["apiVersion"] = typeof( ApiVersionRouteConstraint ) } }; config.MapHttpAttributeRoutes(constraintResolver); config.AddApiVersioning(o => o.AssumeDefaultVersionWhenUnspecified = true); [ApiVersion("2.05")] [RoutePrefix("api/v{version:apiVersion}/ger")] public class caGerController [Route("~/api/ger/getDetail")] [Route("getDetail")] GetGerData [ApiVersion("1")] [RoutePrefix("api/v{version:apiVersion}/gerDetail")

Web API 2.2, Inherited Controller with Route Overrides (Is this possible)?

瘦欲@ 提交于 2020-01-16 05:17:30
问题 So I have a generic base controller implementing CRUD, which derives from APIController . public class GenericController<TEntity> : ApiController where TEntity : class { private readonly GenericModel<TEntity> _model; public IModel Model { get { return _model; } } public GenericController(IGenericModel<TEntity> model) { _model = (GenericModel<TEntity>)model; } [HttpPost, Route] public IHttpActionResult Post(TEntity entity) { TEntity newEntity = _model.Create(entity); String urlLink = $"

C# - Cannot call a HttpConfiguration Extension Methods

大憨熊 提交于 2020-01-16 04:49:06
问题 I cannot call an HttpConfiguration extension method: using System.Configuration; using System.Web.Http; ... var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); // <-- error Error: 'System.Web.Http.HttpConfiguration' does not contain a definition for 'MapHttpAttributeRoutes' and no extension method 'MapHttpAttributeRoutes' accepting a first argument of type 'System.Web.Http.HttpConfiguration' could be found (are you missing a using directive or an assembly reference?) I

How can I safely handle invalid requests for repository data?

眉间皱痕 提交于 2020-01-14 19:42:09
问题 With this code: public String Get(int id) { return platypi.Find(p => p.Id == id).Name; } ...I can get existing data via: http://localhost:33181/api/DPlatypus/N (where N corresponds to an existing ID). If I use a nonexistent value, though, it blows up. So, I tried this: public String Get(int id) { if (!string.IsNullOrEmpty(platypi.Find(p => p.Id == id).Name)) { return platypi.Find(p => p.Id == id).Name; } return string.Empty; } ...but it has no beneficial effect. Is there a way to safely

How to create and update a relation between documents in DocumentDB using ASP.NET Web API

穿精又带淫゛_ 提交于 2020-01-14 05:22:13
问题 I am new to .NET and Azure, and am trying to create a simple Web API, to help me learn. I have two collections of DocumentDB documents. The documents in each collection are defined as follows: public class Log { [JsonProperty(PropertyName = "id")] public string Id { get; set; } [JsonProperty(PropertyName = "studentName")] public string StudentName { get; set; } [JsonProperty(PropertyName = "assignment")] public string Assignment { get; set; } [JsonProperty(PropertyName = "dueDate")] public

ASP.NET WebAPI Supported Media Types per Method

可紊 提交于 2020-01-13 10:21:49
问题 Given a method in a controller: public class CustomerController : ApiController { [HttpGet] public CustomerDto GetById([FromUri] int id) { . . return customerDto } } Is there a way to specify supported Media Types with an attribute? For instance, CustomerDto is a complex class and will only serialize with JSON (application/json) not XML (application/xml) but could also accept PDF (application/pdf). Is there something like this: [HttpGet(Accepts.JSON, Accepts.PDF)] or [HttpGet][AcceptJSON]

A route named 'DefaultApi' is already in the route collection

我只是一个虾纸丫 提交于 2020-01-13 09:19:08
问题 This question may seems duplicate but this is slightly different. In all other question in SO I had noticed that they have multiple routes registered. but in my case I have just one route. I am creating asp.net webapi (framework 4.5) and have just one route in RegisterRoutes() method - public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "DefaultApi", url: "rest/{controller}/{id}", defaults: new { id =