Tag: asp.net web api routing

需要我的web api 2控制器的路由

我有一个简单的WebApi2控制器,它返回XML,但我无法使用我定义的路由正确添加另一个方法: 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 HttpResponseMessage GetGrantsIS() { XmlDocument docContent = GetXmlDataFromDB(); return new HttpResponseMessage { Content = new StringContent(docContent.InnerXml.ToString(), […]

控制器动作命名约定

正如命名约定所说,WebApi控制器动作名称应为Get(),Put()。 Post()等。但是告诉我,如果我有一个控制器作为CustomerController ,现在我想在其中有两个动作。 一个是GetCustomerById(int id) ,另一个是GetCustomerByAge(int age) 。 这两个动作都接受一个参数作为int。 所以,如果我想让url用户友好,比如“api / customer /”,我也想遵循命令约定,比如Get(int id)/ Get(int age) ,我该怎么做?

如何使用路由属性进行Web API版本控制时获取控制器名称

我需要从我的路由中获取控制器名称,如果在WebApiConfig中使用标准路由代码,我可以这样做。 但是,如果我使用路由属性,它会开始变得有点困难,尤其是在尝试版本时。 示例:如果我调用api / terms / bonus并且我有一个BonusController和BonusV2Controller以及一个BonusV3Controller,这段代码将返回最新的控制器版本3.那没关系,我可以接受最新版本的默认版本。 var attributedRoutesData = request.GetRouteData().GetSubRoutes(); var subRouteData = attributedRoutesData.FirstOrDefault(); var actions = (ReflectedHttpActionDescriptor[])subRouteData.Route.DataTokens[“actions”]; //This seems to get latest controller name. ie. V2 controllerName = actions[0].ControllerDescriptor.ControllerName; 现在,如果我请求版本1,为简单起见,我将使用查询字符串并调用api / terms / bonus?v = 2 所以这段代码不再有效(显然)。 如何获取V2控制器名称? 如果我放弃路由属性并只使用WebApiConfig路由,则此代码可以正常工作。 HttpControllerDescriptor controllerDescriptor = null; var controllers = GetControllerMapping(); var routeData = request.GetRouteData(); var controllerName […]

Swagger 2.0不支持:带路径的多个操作

我在WebApi 2应用程序中集成了swagger。 应用程序有单个控制器时,它工作正常。 当我在应用程序中添加第二个控制器时。 我收到了以下错误: 发生错误。“,”ExceptionMessage“:”Swagger 2.0不支持:路径’api / Credential’和方法’GET’的多个操作。 请参阅配置设置 – \“ResolveConflictingActions \”以获取潜在的解决方法“,”ExceptionType“:”System.NotSupportedException“,”StackTrace“:”在Swashbuckle的Swashbuckle.Swagger.SwaggerGeneratorOptions.DefaultConflictingActionsResolver(IEnumerable 1 apiDescriptions)\r\n at Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable 1 apiDescriptions,SchemaRegistry schemaRegistry)\ r \ n在Swashbuckle.Swagger.SwaggerGenerator。 c__DisplayClass7.b__4(IGrouping 2 group)\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable 1 source,Func 2 keySelector, Func 2 elementSelector,IEqualityComparer 1 comparer)\r\n at Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion)\r\n at Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, […]

如何使用ASP.NET Web API在DocumentDB中创建和更新文档之间的关系

我是.NET和Azure的新手,我正在尝试创建一个简单的Web API,以帮助我学习。 我有两个DocumentDB文档集合。 每个集合中的文件定义如下: 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 DateTime DueDate { get; set; } [JsonProperty(PropertyName = “goal”)] public string Goal { […]

C# – 无法调用HttpConfiguration扩展方法

我无法调用HttpConfiguration扩展方法: using System.Configuration; using System.Web.Http; … var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); // <– error 错误:’System.Web.Http.HttpConfiguration’不包含’MapHttpAttributeRoutes’的定义,并且没有可以找到接受类型’System.Web.Http.HttpConfiguration’的第一个参数的扩展方法’MapHttpAttributeRoutes’(你错过了吗?使用指令或程序集引用?) 我尝试使用System.Web.Http版本4和5,以及“.NET Framework 4.5.2”和“.NET Framework 4”。 我错过了什么? 参考

Web API 2路由属性在一个控制器中工作,但不在另一个控

使用.NET 4.5.1,Web API 2,Visual Studio 2013: 我有一个Web API,它有以下路由…… / API /提供商/特色菜 / API /提供商/特色/ 123 / API /供应商/专业/医学 这些按预期工作……第一个获得所有专业的列表,第二个获得专业ID 123,第三个获得名称中的“药”所有专业。 我也有这些路线…… / API /位置/特色菜 / API /位置/特色/ 123 / API /位置/特色/邻 只有最后两个工作…第一个返回此错误: No HTTP resource was found that matches the request URI […] No route providing a controller name was found to match request URI […]

名为“DefaultApi”的路径已在路径集合中

这个问题可能看似重复,但这有点不同。 在SO的所有其他问题中,我注意到他们注册了多条路线。 但就我而言,我只有一条路线。 我正在创建asp.net webapi(框架4.5)并且在RegisterRoutes()方法中只有一个路由 – public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute( name: “DefaultApi”, url: “rest/{controller}/{id}”, defaults: new { id = UrlParameter.Optional } ); } 那为什么会抛出错误? A route named ‘DefaultApi’ is already in the route collection. Route names must be unique. Parameter name: name

不同的RoutePrefix,相同的控制器名称

我在使用命名空间和RoutePrefix将我的web-api应用程序拆分到不同的区域(而不是mvc区域)时遇到问题 该应用程序使用Owin Self Host托管,在我的Startup类中,我有以下内容。 HttpConfiguration config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); app.UseWebApi(config); 我测试过的两个控制器 [RoutePrefix(“api/test”)] public class TestController : ApiController { [Route(“”)] public IHttpActionResult Get() { return Ok(“api”); } } [RoutePrefix(“sync/test”)] public class TestController : ApiController { [Route(“”)] public IHttpActionResult Get() { return Ok(“sync”); } } 这两个控制器存在两个不同的命名空间,Api和Sync。 当我尝试使用http:// localhost / api / test和http:// localhost / api / […]

访问DelegatingHandler中的ApiController类型

我正在使用Asp.Net WebAPI进行项目。 我目前正在进行身份validation和授权。 我有一个messageHandler,它将检查请求的HTTP身份validation标头并构建我的身份和用户配置文件。 但是,我想用操作可能需要的声明来注释我的控制器操作(或仅控制器)(我们有很多用户可以拥有的声明,所以我不想全部加载它们)。 例如: public class MyController : ApiController { [LoadClaims(“SomeClaim”, “SomeOtherClaim”, “etc”)] public string Get() { if (HasClaim(“SomeClaim”)) return “Awesome”; return “Bummer”; } } 在身份validation消息处理程序中,我希望能够查看属性并根据所需内容从数据库中恢复声明。 为此,我需要知道我将根据路线命中的控制器和动作: protected override Task SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { … var routeData = request.GetRouteData(); object controllerName; object actionName; routeData.Values.TryGetValue(“controller”, out controllerName); … 所以我可以做到。 但是现在我需要把它变成一个我可以反思的Type,但我所拥有的只是控制器名称(甚至不是完整的类名或命名空间)。 我怎样才能把它变成我可以反思的东西以获得属性等? 我正在研究DefaultHttpControllerSelector来看看WebAPI堆栈是如何做到的,它似乎使用了HttpControllerTypeCache […]