Tag: asp.net web api routing

仅为路由公开.NET OData API的子集(对于排除的API,返回404)

背景/环境: 我们有两条路线,路线前缀不同: 路由1前缀: /api 路由2前缀: /api/partial 目前,我们对两个路由前缀使用相同的EdmModel。 (参见第一个代码snippit,名为“我们目前正在做什么”)。 我们想要什么: 我们只需要为Route 2允许APIfunction的子集: /api/partial 。 当有人试图访问“部分”EdmModel不可用的API时,我们希望返回404 例: 我们想为/api/parial/products返回404 ,其中products未在此“部分”API路径中定义。 我们仍希望将/api/products路由到控制器方法 我们尝试过的: 使用第二个EdmModel,它只包含完整EdmModel中可用实体的子集。 (参见第二个代码snippit,名为“我们想做什么:”。) 问题: 我们在服务启动时收到错误: The path template ‘products’ on the action ‘Export’ in controller ‘Products’ is not a valid OData path template. Resource not found for the segment ‘products’.) The path template ‘products’ on the action […]

Web Api Controller在其他项目中,路由属性不起作用

我有两个项目的解决方案。 一个Web Api bootstap项目,另一个是类库。 类库包含一个带属性路由的ApiController。 我将web api项目中的引用添加到类库中,并希望它能够正常工作。 Web api中的路由配置为: config.MapHttpAttributeRoutes(); 控制器很简单,看起来像: public class AlertApiController:ApiController { [Route(“alert”)] [HttpGet] public HttpResponseMessage GetAlert() { return Request.CreateResponse(HttpStatusCode.OK, “alert”); } } 但是当我转到url“/ alert”时,我得到了404。 我在这里想念的是什么? 为什么我不能使用这个控制器? 组装肯定是加载所以我不认为http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/是答案这里。 有任何想法吗?

在Web API中的id参数之后使用操作进行路由

在web api中,默认路由是: /api/locations/123?days=5 config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional } ); 但是,如果我希望路径看起来像这样/api/locations/123/events?days=5同时仍然可以使用类似这样的路径命中LocationsController /api/locations/123?state=md 控制器: public class LocationsController : ApiController { // GET api/locations/123/events?days=5 public IEnumerable GetEventsByDays(int idLocation, int days) { // do stuff } // GET api/locations/123?state=md public IEnumerable GetLocationsByState(string state) { // do stuff } } 这里真的有两个问题: 有一个返回事件的LocationsController或者是否应该有一个完全独立的控制器是否有意义? 如何设置WebApiConfig的路由以允许这样的路由?

控制器上的多个路由

想知道是否有可能有多个路由指向WebApi控制器? 例如,我想同时将http:// domain / calculate和http:// domain / v2 / calculate指向同一个控制器函数?

元数据与WebAPi OData属性路由不起作用

我正在为OData端点使用OData属性路由。 这是我的一个例子: [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); } } 这是我的设置: config.MapODataServiceRoute(“odata”, […]

.Net WebApi OData返回Queryable的操作

我希望实现接近RateProduct操作的内容: http : //www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions 在该教程中,它被定义为: [HttpPost] public int RateProduct([FromODataUri] int key, ODataActionParameters parameters) { // … } ODataModelBuilder modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet(“Products”); // New Code ActionConfiguration rateProduct = modelBuilder.Entity().Action(“RateProduct”); rateProduct.Parameter(“Rating”); rateProduct.Returns(); 但是,我有一个Location实体的用例,它足够聪明,可以返回其周围某个半径范围内的其他位置。 它大致应该是这样的: [HttpPost] public IQueryable GetLocationsWithinRadius([FromODataUri] int key, ODataActionParameters parameters) { // Get the Location instance intended to be the center of the […]

Web API 2 / MVC 5:属性路由将参数作为查询字符串传递,以在同一控制器上定位不同的操作

我一直在使用新的Web API 2(看起来非常有前途)但我有点头疼让一些路由工作。 当我有GetAllUsers / GetUser(int id)时,一切正常,但是当我添加GetUserByName(字符串名称)和/或GetUserByUsername(字符串用户名)时,事情开始令人毛骨悚然。 我知道int将是第一个,我可以重新排序路线,但让我们想象下面的场景: 用户可以拥有有效的username=1234或者name=1234 (我知道它不太可能,但我们需要防止任何可能的情况)我们可能在数据库中有一个有效的1234 ID,并且所有路由都将被混淆。 也许这是我们需要在新的WebAPI 2上使用的东西,所以我想我可以带来一个“解决方法”,将filter作为查询字符串传递给同一个控制器中的不同操作,例如api/users/?username=1234 (GetUserByUsername)或api/users/?name=1234 (GetUserByName) 但我不能让查询字符串通过……实际上上面的任何查询字符串选项都被GetAllUsers捕获。 有没有人对这种情况有任何建议/解决方案? 非常感谢

Web API路由以支持GUID和整数ID

如何支持GUID和整数的GET路由? 我意识到GUID并不理想,但它现在就是它。 我想添加对整数的支持,以便用户更容易记住和传达应该是唯一的“密钥”。 示例路线: testcases/9D9A691A-AE95-45A4-A423-08DD1A69D0D1 testcases/1234 我的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 { action = “Get” }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get), key = @”^\d+$” }); routes.MapHttpRoute(“DefaultApiWithId”, “Api/{controller}/{id}”, new { action = “Get” }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }); routes.MapHttpRoute(“DefaultApiGet”, […]

如何将多个args传递/接收到RESTful Web API GET方法?

接收参数(返回标量值而不是数据集)的GET RESTful方法的常见示例如下所示: public string Get(int id) { //get and return the value } …传递的val通常是一个ID,因此您可以使用它来获取基于该唯一值的标量值。 但是,如果要传递多个值,例如字符串和int,该怎么办? 这只是定义一个这样的方法的问题: public string Get(string someString, int someInt) { //get and return the value } ……并且这样称呼它: //const string uri = “http://192.112.183.42:80/api/platypusItems/someString/someInt”;, zB: const string uri = “http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42”; var webRequest = (HttpWebRequest) WebRequest.Create(uri); ? IOW,路由机制是否会发现,由于传递了两个args,它应该使用两个args(“约定优于配置”)调用Get()方法,还是需要做更多的事情才能正确地路由事物?

+(加号)登录Web API路由

我正在使用asp.net web api项目,我必须通过post传递手机号码。 但我不能返回一个加号。 我的路线: config.Routes.MapHttpRoute( name: “SmsRoute”, routeTemplate: “rest/sms/{country}/{company}/phone/{mobilenumber}”, defaults: new { controller = “Sms”, action = “PostSms” }); 控制器: public HttpResponseMessage PostSms(string country, string company, string mobilenumber) { return Request.CreateResponse( HttpStatusCode.Created ); } 当我在fiddler上运行这个post方法时: http://index.html/rest/sms/se/company/phone/46700101010/messages/out 我收到了这个回复: 但我希望它能够向我展示加号。