部署到IIS 7.5后的空白自定义404页面 [英] Blank custom 404 page after deploying to IIS 7.5
问题描述
不正确的网址,如 http:// localhost:54321 / foo / bar 和错误,如 HttpNotFound()
在我的本地主机处理没有问题。部署到IIS 7.5之后,它返回上述两种情况下的空白页面。我已经看到别人有这个问题这里(这是Moulde的评论与几个upvote)。代码:
Web.Config
<&的System.Web GT;
< customErrors mode =OffredirectMode =ResponseRedirect>
< error statusCode =404redirect =〜/ Error / NotFound/>
< / customErrors>
< /system.web>
< httpErrors errorMode =自定义existingResponse =替换>
< remove statusCode =404/>
< error statusCode =404responseMode =ExecuteURLpath =/ Error / NotFound/>
< remove statusCode =500/>
< error statusCode =500responseMode =ExecuteURLpath =/ Error / Error/>
< / httpErrors>
ErrorController
public class ErrorController:Controller
{
public ActionResult NotFound(){
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
return View();
}
}
RouteConfig
routes.MapRoute(
Error - 404,
NotFound,
new {controller =Error,action =NotFound}
);
routes.MapRoute(
Error - 500,
Error,
new {controller =Error,action =Error}
);
routes.MapRoute(
name:Default,
url:{controller} / {action} / {id},
defaults:new {controller =Home ,action =Index,id = UrlParameter.Optional}
);
NotFound
< h2> 404未找到< / h2>
< div class =alert alert-info>
< p>< strong> Whoops ...< / strong>< / p>
< p>您要查找的资源不存在< / p>
< / div>
服务器运行Windows Server 2008 R2。当我去... / foo / bar或一个操作返回 HttpNotFound()
,它返回一个空白页。任何想法?
首页/测试
public class HomeController:Controller {
// ...
public ActionResult Test(){
return HttpNotFound();
}
}
更新:
当我去服务器上的 / Error / NotFound
时,它返回一个空白页以及404。与Fiddler的回应:
HTTP / 1.1 404未找到
缓存控制:私人
内容类型:text / html; charset = utf-8
服务器:Microsoft-IIS / 7.5
X-AspNetMvc版本:5.2
X-AspNet版本:4.0.30319
X-SourceFiles:=? UTF-8?B' QzpccWhlXFByb2plY3RzXFZlbmRvclBvcnRhbFxWZW5kb3JQb3J0YWxcRXJyb3JcTm90Rm91bmQ =?=
X-Powered by:ASP.NET
日期:星期四,2015年4月23日13:24:49 GMT
内容长度:5434
更新2:
如果我采取 existingResponse =Replace
,我可以在服务器上打开Error / NotFound。但是仍然在 MyServer / MyProject / foo / bar
在我的情况下,原因是在IIS - 网页设置 - 请求过滤 - SQL注入过滤器...
如果url或查询字符串包含;
,响应以状态404.19结束 - 过滤规则被拒绝。然后显示空白页。 IIS重定向到这样的自定义错误 http://server.com/ Error404.aspx?404; http://server.com/missingURL 和char ;
是为什么请求过滤器由于SQL注入而结束请求。 p>
Incorrect urls like http://localhost:54321/foo/bar and errors like HttpNotFound()
are handled in my localhost without problems. After deploying to IIS 7.5, it return a blank page on both of the above scenarios. I've seen someone else had this problem Here(It was the comment from Moulde with several upvotes). The code:
Web.Config
<system.web>
<customErrors mode="Off" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="~/Error/NotFound"/>
</customErrors>
</system.web>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound"/>
<remove statusCode="500" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error/Error"/>
</httpErrors>
ErrorController
public class ErrorController : Controller
{
public ActionResult NotFound() {
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
return View();
}
}
RouteConfig
routes.MapRoute(
"Error - 404",
"NotFound",
new { controller = "Error", action = "NotFound" }
);
routes.MapRoute(
"Error - 500",
"Error",
new { controller = "Error", action = "Error" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
NotFound
<h2>404 Not Found</h2>
<div class="alert alert-info">
<p><strong>Whoops...</strong></p>
<p>The resource you were looking for doesn't exist</p>
</div>
The server runs Windows Server 2008 R2. When I go to .../foo/bar or an action returns HttpNotFound()
, it returns a blank page. Any idea?
Home/Test
public class HomeController: Controller{
// ...
public ActionResult Test(){
return HttpNotFound();
}
}
Update:
When I go to /Error/NotFound
on server, it returns a blank page as well with 404. The response with Fiddler:
HTTP/1.1 404 Not Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B? QzpccWhlXFByb2plY3RzXFZlbmRvclBvcnRhbFxWZW5kb3JQb3J0YWxcRXJyb3JcTm90Rm91bmQ=?=
X-Powered-By: ASP.NET
Date: Thu, 23 Apr 2015 13:24:49 GMT
Content-Length: 5434
Update 2:
If I take existingResponse="Replace"
away, I can open Error/NotFound on the server. But still got blank page on MyServer/MyProject/foo/bar
In my case the reason was in IIS - web settings - Request Filtering - SQL injection filter...
If url or query string contains ;
, response ends with status 404.19 - Denied by filtering rule. Then blank page is displayed. IIS redirect to custom error like this http://server.com/Error404.aspx?404;http://server.com/missingURL and the char ;
is why Request Filter end your request because of SQL injection.
这篇关于部署到IIS 7.5后的空白自定义404页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!