asp.net-web-api – AttributeRouting不能与HttpConfiguration对
发布时间:2020-12-16 00:01:38 所属栏目:asp.Net 来源:网络整理
导读:我按照这里概述的想法创建了一些集成测试: http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/ 当我尝试从手工制作的HttpConfiguration对象注册路由时,我收到以下错误: “路由模板’api / Contacts / {id}’
我按照这里概述的想法创建了一些集成测试:
http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/ 当我尝试从手工制作的HttpConfiguration对象注册路由时,我收到以下错误: 示例代码: [RoutePrefix("api")] public class ContactsController : ApiController { [GET("Contacts/{id}",RouteName="GetContactsById")] public ContactDTO Get(int id) { return new ContactDTO{ ID =1,Name="test"}; } } } TestClass(MSTest): [TestClass] public class ContactsTest { private string _url = "http://myhost/api/"; private static HttpConfiguration config = null; private static HttpServer server = null; private HttpRequestMessage createRequest(string url,string mthv,HttpMethod method) { var request = new HttpRequestMessage(); request.RequestUri = new Uri(_url + url); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mthv)); request.Method = method; return request; } private HttpRequestMessage createRequest<T>(string url,HttpMethod method,T content,MediaTypeFormatter formatter) where T : class { HttpRequestMessage request = createRequest(url,mthv,method); request.Content = new ObjectContent<T>(content,formatter); return request; } [ClassInitializeAttribute] public static void ClassInitialize(TestContext ctx) { config = new HttpConfiguration(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; config.Services.Replace( typeof(IDocumentationProvider),new DocProvider()); config.Services.Replace( typeof(IApiExplorer),new VersionedApiExplorer(config)); config.Services.Replace( typeof(IHttpControllerSelector),new VersionHeaderVersionedControllerSelector (config) ); AttributeRoutingHttpConfig.RegisterRoutes(config.Routes); WebApiConfig.Register(config); server = new HttpServer(config); } public static void ClassCleanup() { config.Dispose(); server.Dispose(); } [TestMethod] public void RetrieveContact() { var request = createRequest("Contacts/12","application/json",HttpMethod.Get); var client = new HttpClient(server); using (HttpResponseMessage response = client.SendAsync(request).Result) { Assert.IsNotNull(response.Content); } } } 该错误发生在“client.SendAsync”行上.我检查了config.Routes和”inboundHttpMethod’的“Constraints”的数据类型是AttributeRouting.Web.Http.WebHost.Constraints.InboundHttpMethodConstraint 解决方法
有同样的问题.在这里找到答案:
https://github.com/mccalltd/AttributeRouting/issues/191 你需要更换 AttributeRoutingHttpConfig.RegisterRoutes(config.Routes); 同 config.Routes.MapHttpAttributeRoutes(cfg => { cfg.InMemory = true; cfg.AutoGenerateRouteNames = true; cfg.AddRoutesFromAssemblyOf<ContactsController>();// Or some other reference... }); 我发现我也需要AutoGenerateRouteNames部分. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 将经典的asp页面映射到IIS中的.net
- 如何让ASP.NET AJAX通过GZip压缩发送JSON响应?
- CookieAuthenticationOptions,ExpireTimeSpan不起作用
- 实体框架 – EF Codefirst和RDLC Reports
- asp.net-mvc-3 – 数据注释MVC3必需属性
- asp.net-mvc-4 – MVC 4 – 从视图中的Controller -Show返回
- asp.net – 404处理程序获取“句柄未初始化”异常
- asp.net-mvc – 如何在MVC5中从AccountController模拟Appli
- Linq高级查询
- ASP.NET Core2.2+Quartz.Net 实现web定时任务
推荐文章
站长推荐
- asp.net – 在Global.asax文件中创建Timer
- asp.net HC架构
- 为ASP.NET网站创建DAL
- asp.net-web-api – ASP.NET Web API自定义帮助页
- asp.net-mvc – 是否可以在按钮点击时“交换”部
- 如何从asp.net HyperLink控件调用javascript
- asp.net-mvc – ASP.net MVC 3项目文件没有出现在
- asp.net-mvc-3 – 如何使用dotnet highcharts dl
- asp.net-mvc – 仅当不使用角色时,如何重定向[Au
- 在ASP.NET MVC中的jQuery与MicrosoftAjax
热点阅读