ASP.NET Web API内存中测试中的内部服务器错误
发布时间:2020-12-16 03:39:21 所属栏目:asp.Net 来源:网络整理
导读:在 in-memory test中测试ASP.NET Web API控制器时,我收到“内部服务器错误”(状态码500). [TestFixture]public class ValuesControllerTest{ private HttpResponseMessage response; [TestFixtureSetUp] public void Given() { var config = new HttpConfigu
在
in-memory test中测试ASP.NET Web API控制器时,我收到“内部服务器错误”(状态码500).
[TestFixture] public class ValuesControllerTest { private HttpResponseMessage response; [TestFixtureSetUp] public void Given() { var config = new HttpConfiguration { IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always }; config.Routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { controller = typeof(ValuesController).Name.Replace("Controller",string.Empty),id = RouteParameter.Optional } ); //This method will cause internal server error but NOT throw any exceptions //Remove this call and the test will be green ScanAssemblies(); var server = new HttpServer(config); var client = new HttpClient(server); response = client.GetAsync("http://something/api/values/5").Result; //Here response has status code 500 } private void ScanAssemblies() { PluginScanner.Scan(".",IsApiController); } private bool IsApiController(Type type) { return typeof (ApiController).IsAssignableFrom(type); } [Test] public void Can_GET_api_values_5() { Assert.IsTrue(response.IsSuccessStatusCode); } } public static class PluginScanner { public static IEnumerable<Type> Scan(string directoryToScan,Func<Type,bool> filter) { var result = new List<Type>(); var dir = new DirectoryInfo(directoryToScan); if (!dir.Exists) return result; foreach (var file in dir.EnumerateFiles("*.dll")) { result.AddRange(from type in Assembly.LoadFile(file.FullName).GetTypes() where filter(type) select type); } return result; } } 我已经将Visual Studio配置为在抛出任何.Net异常时中断.代码在任何异常时都不会停止,也不能在响应中找到任何异常详细信息. 我该怎么做才能看到导致“内部服务器错误”的原因? 解决方法
Response.Content中有例外
if (Response != null && Response.IsSuccessStatusCode == false) { var result = Response.Content.ReadAsStringAsync().Result; Console.Out.WriteLine("Http operation unsuccessfull"); Console.Out.WriteLine(string.Format("Status: '{0}'",Response.StatusCode)); Console.Out.WriteLine(string.Format("Reason: '{0}'",Response.ReasonPhrase)); Console.Out.WriteLine(result); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在ASP.NET MVC中实现Disqus?
- asp.net-mvc – 模型绑定不起作用
- asp.net-mvc – 根据请求域捆绑CSS文件?
- 如何为ASP.NET WebAPI 2中使用路由属性的特定控制器添加Mes
- ASP.NET GridView用法(二) 模板,脚注,排序,编辑,删除
- asp.net-mvc – asp.net web farm中丢失的会话状态
- asp.net-web-api2 – 使用未绑定功能时具有多个路由的OData
- asp.net – 如果启用ASP .Net请求验证,是否需要HtmlEncode
- asp.net – requestValidationMode 4.5 vs 2.0
- asp.net – Databound和Databind之间的区别
推荐文章
站长推荐
- asp.net – 添加context.Response.Headers.Add(“
- ASP.NET MVC下的四种验证编程方式
- asp.net-mvc – 编译时mvc视图检查与msbuild
- ASP.NET Web Pages - HTML 窗体
- asp.net – MVC2 – 逻辑应该去的基本问题
- asp.net-web-api – webapi中的attributerouting
- asp.net-mvc-3 – 使用csvhelper(nuGET)和C#MVC导
- 如何在asp.net/c#中重现丢失的会话?
- asp.net-mvc – 在MVC控制器中复制代码
- 如何在ASP.NET 5中添加一个TypeScript绝对类型的
热点阅读