404响应追加到的WebAPI响应

问题描述:

我设法让两个响应单个Web请求:404响应追加到的WebAPI响应

  1. 我的WebAPI响应(一个简单的字符串200个状态码)
  2. 404,从什么样子一个静态文件处理程序从铬

截图:

Screen shot

我的设置是:

  • F#
  • 单在Ubuntu 15.10
  • xsp4作为Web服务器
  • Owin作为路由引擎

正如你从截图中可以看到在浏览器响应的第一行打印出正确的响应(有时是叹息),然后立即在页面上通过标准404页面进行响应。

的启动代码:

type Startup() = 

    static member RegisterWebApi(config: HttpConfiguration) = 
    // Configure routing 
    config.MapHttpAttributeRoutes() 
    // Configure serialization 
    config.Formatters.XmlFormatter.UseXmlSerializer <- true 
    config.Formatters.JsonFormatter.SerializerSettings.ContractResolver <- Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() 

    member __.Configuration(builder: IAppBuilder) = 
    let config = new HttpConfiguration() 
    Startup.RegisterWebApi(config) 
    builder.UseWebApi(config) |> ignore 
    builder.UseErrorPage() |> ignore 

控制器代码:

[<RoutePrefix("api")>] 
type WordsearchController() = 
    inherit ApiController() 

    [<Route("")>] 
    member this.Get() = 
    this.Request.CreateResponse<string>(HttpStatusCode.OK, "This is my simple output") 

我曾尝试在web.config中删除静态文件处理程序,在启动时使用MVC样式路由映射,以及各种路由属性为控制器。

它似乎像webapi路由和静态文件处理程序都把他们的两分钱到响应,而不是一个覆盖另一个。

由于每从@马克请求,所述请求是一个简单的GET到http://localhost:9000/api,并且响应是:

HEADER

HTTP/1.0 404 Not Found 
Date: Mon, 18 Jan 2016 00:11:17 GMT 
Server: Mono.WebServer.XSP/4.2.0.0 Linux 
X-AspNet-Version: 4.0.30319 
Content-Length: 5085 
Cache-Control: private 
Content-Type: text/html 
Keep-Alive: timeout=15, max=94 
Connection: Keep-Alive 

BODY

GET api 

404 Not Found 

localhost:9000 

5.0 KB 

127.0.0.1:9000 


2ms 
HeadersResponseHTMLCache 

<string>This is my simple output</string><?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional 
.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<style type="text/css"> 
body { background-color: #FFFFFF; font-size: .75em; font-family: Verdana, Helvetica, Sans-Serif; margin 
: 0; padding: 0; color: #696969; } 
a:link { color: #000000; text-decoration: underline; } 
a:visited { color: #000000; } 
a:hover { color: #000000; text-decoration: none; } 
a:active { color: #12eb87; } 
p, ul { margin-bottom: 20px; line-height: 1.6em; } 
pre { font-size: 1.2em; margin-left: 20px; margin-top: 0px; } 
h1, h2, h3, h4, h5, h6 { font-size: 1.6em; color: #000; font-family: Arial, Helvetica, sans-serif; } 

h1 { font-weight: bold; margin-bottom: 0; margin-top: 0; padding-bottom: 0; } 
h2 { font-size: 1em; padding: 0 0 0px 0; color: #696969; font-weight: normal; margin-top: 0; margin-bottom 
: 20px; } 
h2.exceptionMessage { white-space: pre; } 
h3 { font-size: 1.2em; } 
h4 { font-size: 1.1em; } 
h5, h6 { font-size: 1em; } 
#header { position: relative; margin-bottom: 0px; color: #000; padding: 0; background-color: #5c87b2 
; height: 38px; padding-left: 10px; } 
#header h1 { font-weight: bold; padding: 5px 0; margin: 0; color: #fff; border: none; line-height: 2em 
; font-family: Arial, Helvetica, sans-serif; font-size: 32px !important; } 
#header-image { float: left; padding: 3px; margin-left: 1px; margin-right: 1px; } 
#header-text { color: #fff; font-size: 1.4em; line-height: 38px; font-weight: bold; } 
#main { padding: 20px 20px 15px 20px; background-color: #fff; _height: 1px; } 
#footer { color: #999; padding: 5px 0; text-align: left; line-height: normal; margin: 20px 0px 0px 0px 
; font-size: .9em; border-top: solid 1px #5C87B2; } 
#footer-powered-by { float: right; } 
.details { font-family: monospace; border: solid 1px #e8eef4; white-space: pre; font-size: 1.2em; overflow 
: auto; padding: 6px; margin-top: 6px; background-color: #eeeeff; color: 555555 } 
.details-wrapped { white-space: normal } 
.details-header { margin-top: 1.5em } 
.details-header a { font-weight: bold; text-decoration: none } 
p { margin-bottom: 0.3em; margin-top: 0.1em } 
.sourceErrorLine { color: #770000; font-weight: bold; } 
</style> 
<script type="text/javascript"> 
    var hideElementsById = new Array(); 
    window.onload = function() { 
     if (!hideElementsById || hideElementsById.length < 1) 
      return; 
     for (index in hideElementsById) 
      toggle (hideElementsById [index]); 
    } 

    function toggle (divId) 
    { 
     var e = document.getElementById (divId); 
     if (!e) 
      return; 
     var h = document.getElementById (divId + "Hint"); 
     if (e.style.display == "block" || e.style.display == "") { 
      e.style.display = "none"; 
      if (h) 
       h.innerHTML = " (click to show)"; 
     } else { 
      e.style.display = "block"; 
      if (h) 
       h.innerHTML = " (click to hide)"; 
     } 
    } 
</script> 
<title>Error 404</title> 
</head> 
<body> 
<div class="page"> 
<div id="header"> 
<div id="header-text">Application Exception</div> 
</div> 
<div id="main"> 
    <h1>System.Web.HttpException</h1> 
    <h2 class="exceptionMessage">The resource cannot be found.</h2> 
    <p><strong>Description:</strong> HTTP 404.The resource you are looking for (or one of its dependencies 
) could have been removed, had its name changed, or is temporarily unavailable. Please review the following 
URL and make sure that it is spelled correctly.</p><p><strong>Details:</strong> Requested URL: /api 
</p> 
<div><strong>Exception stack trace:</strong></div> 
<div class="details"> at System.Web.StaticFileHandler.ProcessRequest (System.Web.HttpContext context 
) &lt;0x403b0a40 + 0x00733&gt; in &lt;filename unknown&gt;:0 
    at System.Web.DefaultHttpHandler.BeginProcessRequest (System.Web.HttpContext context, System.AsyncCallback 
callback, System.Object state) &lt;0x403b0240 + 0x00153&gt; in &lt;filename unknown&gt;:0 
    at System.Web.HttpApplication+&lt;Pipeline&gt;c__Iterator1.MoveNext() &lt;0x40332110 + 0x04416&gt 
; in &lt;filename unknown&gt;:0 
    at System.Web.HttpApplication.Tick() &lt;0x40330c60 + 0x00057&gt; in &lt;filename unknown&gt;:0 < 
/div><div id="footer"> 
    <div style="color:Black;"><strong>Version Information:</strong> <tt>4.2.1 (Stable 4.2.1.102/6dd2d0d 
Thu Nov 12 09:52:44 UTC 2015)</tt>; ASP.NET Version: <tt>4.0.30319.17020</tt></div> 
    <div id="footer-powered-by">Powered by <a href="http://mono-project.com/">Mono</a></div> 
</div> 
</div> 
</div> 
</body> 
</html> 

<!-- 
[System.Web.HttpException]: Path &#39;/api&#39; was not found. 
    at System.Web.StaticFileHandler.ProcessRequest (System.Web.HttpContext context) <0x403b0a40 + 0x00733 
> in <filename unknown>:0 
    at System.Web.DefaultHttpHandler.BeginProcessRequest (System.Web.HttpContext context, System.AsyncCallback 
callback, System.Object state) <0x403b0240 + 0x00153> in <filename unknown>:0 
    at System.Web.HttpApplication+<Pipeline>c__Iterator1.MoveNext() <0x40332110 + 0x04416> in <filename 
unknown>:0 
    at System.Web.HttpApplication.Tick() <0x40330c60 + 0x00057> in <filename unknown>:0 
--> 


1 request 

5.0 KB 

2ms (onload: 88ms) 
+0

控制器代码是否实际格式化为发布?因为如果是这样,我不明白它是如何编译的。鉴于F#对空白敏感,发布代码时请确保格式正确。 –

+0

请发布实际的HTTP流量(请求和响应),而不是张贴屏幕快照。从屏幕截图中,我看不到响应*真正*。 HTTP 1.1是纯文本,因此您可以在此处轻松发布。 –

+0

谢谢@mark。我已经添加了详细的回复并在代码中修正了缩进。谢谢你的时间! –

单不支持的WebAPI异步管道。

我提出单声道请求https://github.com/mono/mono/pull/3048。它使同步执行异步Web api堆栈。

现在,您可以使用WebAPI异步管道和Mono的此修复程序。

+0

谢谢奥列格。我等待合并并释放出诱人的气息。 –