相关疑难解决方法(0)

是否可以使用自定义错误页面与MVC站点而不是Web API?

我有一个带有/ api文件夹的MVC项目,其中包含我的Web API控制器.我想要以下事项:

  • 我的MVC站点在发生错误时提供自定义错误页面
  • 我的Web API提供默认错误响应(包含异常和堆栈跟踪的json/xml)

在我的MVC站点的web.config中,我有一个httpErrors节点,并将errorMode设置为"Custom",以便在404s/500s/etc时我可以拥有漂亮的错误页面.在浏览MVC站点期间发生:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <remove statusCode="403" subStatusCode="-1" />
  <error prefixLanguageFilePath="" statusCode="404" path="Content\notfound.htm" responseMode="File" />
  <error statusCode="500" path="/Errors" responseMode="ExecuteURL" />
  <error statusCode="403" path="/Errors/Http403" responseMode="ExecuteURL" /></httpErrors>
Run Code Online (Sandbox Code Playgroud)

但是,使用该配置,API将在发生错误时提供自定义错误页面,而不是具有异常/堆栈跟踪的json/xml(这是所需的行为).

有没有办法将自定义错误配置为仅应用于我的MVC站点而不是Web API?这篇博客说没有(http://blog.kristandyson.com/2012/11/iis-httperrors-aspnet-web-api-fully.html),但我想知道是否有其他人找到了工作自该博客发布以来.

我想如果没有,我可以为我的Web API项目创建一个单独的项目/程序集.这将允许我分别为MVC和Web API配置httpErrors,但我不想创建另一个项目,所以我还有另一个web.config来配置.

iis error-handling asp.net-mvc web-config asp.net-web-api

30
推荐指数
1
解决办法
6633
查看次数

在web.config中指定相对路径

将MVC3 Asp.net应用程序部署到客户端站点后,我们遇到了问题.
在客户端站点中,已在IIS7中创建了我们需要部署的虚拟目录.

问题出在web.config中,我们在其中指定了自定义错误页面

<!-- Custom Error Pages -->
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <remove statusCode="500" subStatusCode="-1" />
      <remove statusCode="403" subStatusCode="-1" />
      <error statusCode="404" path="/Error/Http404" responseMode="ExecuteURL" />
      <error statusCode="403" path="/Error/Http403" responseMode="ExecuteURL" />
      <error statusCode="500" path="/Error/ServerError" responseMode="ExecuteURL" />
    </httpErrors>
Run Code Online (Sandbox Code Playgroud)

错误页面路径无法正常工作.经过调查,我们发现必须指定虚拟目录并设置路径/virtual_directory/Error/Http404

有没有办法可以指定相对于虚拟目录的路径?

asp.net web-config visual-studio-2010 asp.net-mvc-3

5
推荐指数
1
解决办法
1725
查看次数