首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在web.config中使用httpErrors时错误控制器未命中

在web.config中使用httpErrors时错误控制器未命中
EN

Stack Overflow用户
提问于 2014-11-18 19:47:39
回答 1查看 714关注 0票数 1

我正在努力在我的web.config中正确设置httpErrors部分,以便同时捕获ASP.NET MVC errorsIIS errors。我得到了403状态代码和空白页面。我通过在URL中键入不正确的URL和文件名来测试404错误,例如:

代码语言:javascript
代码运行次数:0
运行
复制
www.mywebsite.com/test
www.mywebsite.com/test.html

我使用的是最新版本的ASP.NET MVC 5。此web应用程序在具有使用integrated mode的应用程序池的IIS 7.5上运行。

这是我的web.config在应用程序根目录中的样子(这是我目前拥有的全部内容):

代码语言:javascript
代码运行次数:0
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="webpages:Version" value="3.0.0.0"/>
        <add key="webpages:Enabled" value="false"/>
        <add key="ClientValidationEnabled" value="true"/>
        <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    </appSettings>

    <system.web>
        <customErrors mode="Off" />
        <compilation debug="true" targetFramework="4.5.2"/>
        <httpRuntime targetFramework="4.5.2"/>
    </system.web>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <httpErrors errorMode="Custom" existingResponse="Replace">
            <remove statusCode="404" />
            <remove statusCode="500" />
            <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
            <error statusCode="500" responseMode="ExecuteURL" path="/Error" />
        </httpErrors>
    </system.webServer>

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
                <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
            </dependentAssembly>
            <!-- ...and so forth... -->
        </assemblyBinding>
    </runtime>
</configuration>

我的global.asax.cs文件:

代码语言:javascript
代码运行次数:0
运行
复制
public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

    protected void Application_Error()
    {
        // Break point has been set here for testing purposes
    }

    protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 404)
        {
            // Break point has been set here for testing purposes
        }
    }
}

我的错误控制器:

代码语言:javascript
代码运行次数:0
运行
复制
public class ErrorController : Controller
{
    public ActionResult Index()
    {
        // Break point has been set here for testing purposes
        Response.StatusCode = 500;

        return View();
    }

    public ActionResult Forbidden()
    {
        // Break point has been set here for testing purposes
        Response.StatusCode = 403;

        return View();
    }

    public ActionResult NotFound()
    {
        // Break point has been set here for testing purposes
        Response.StatusCode = 404;

        return View();
    }
}

它们在Views文件夹中有相应的视图。

在我的错误控制器中,我的断点永远不会命中。我不明白为什么?我看过Stackoverflow上的许多例子,这就是每个人都建议我这样做的方式。在给定代码的情况下,什么时候不会到达断点?所有发生的都是一个403错误状态和一个空白页面。命中Application_Error()Application_EndRequest()中的断点,但不命中错误控制器中的断点。

我可以用Application_Error()Application_EndRequest()编写代码,让我设置错误控制器和操作方法,但是如果我可以使用web.config,为什么还要这样做呢?这也应该起作用吗?

EN

回答 1

Stack Overflow用户

发布于 2014-11-18 20:12:03

伙计,你提到statusCode是404,那你怎么能期望是200 OK呢?:)

只需从操作方法中删除Response.StatusCode = 404;

下面的代码应该可以工作

代码语言:javascript
代码运行次数:0
运行
复制
 public ActionResult NotFound()
    {
        return View();
    }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26993606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档