asp.net-web-api – AttributeRouting不能与HttpConfiguration对象一起编写
|
我按照这里概述的想法创建了一些集成测试:
当我尝试从手工制作的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+js实现批量编码与解码的方法
- asp.net – 更改事件和IE8的jQuery问题
- Asp.net MVC – Jquery $.ajax错误回调没有返回responseJSO
- asp.net-mvc – 如何在ASP.NET MVC中实现分页?
- asp.net-mvc-3 – 用于制作直方图的库javascript
- asp.net-mvc-3 – MvcContrib网格和复选框
- asp.net-mvc – 用于MVC 3的Castle Windsor依赖关系解析器
- asp.net-web-api2 – 在WebAPI2项目中加载System.IdentityM
- asp.net – RenderBody和RenderSection之间的区别
- 有AsIFnet标记的#IF DEBUG吗?
- asp.net-core – 当前的运行时目标框架与项目不兼
- asp.net – 防止意外双击按钮
- asp.net – 没有owin.Environment项目在上下文中
- ASP.NET MVC4 WebApi路由中包含文件名
- asp.net-mvc – 使用Ninject时如何处理DBContext
- ASP .NET MVC 3 – 如何提交嵌套在html表单中的a
- asp.net-mvc-3 – 如何增加会话超时MVC 3
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与
- asp.net-mvc – mvc.net如何在迭代列表时使用强类
- asp.net-mvc – 命中错误:在解析器和自定义注册
