如果未写入扩展名,则显示错误页面

Showing error page if extension is not writed

我在我的 Web 应用程序中使用路由模块(在 Global.asax 中)。 我正在写这样的配置;

<customErrors mode="RemoteOnly" defaultRedirect="Error">
  <error statusCode="403" redirect="Error" />
  <error statusCode="404" redirect="Error" />
  <error statusCode="500" redirect="Error" />
</customErrors>

注:错误=Error.aspx

如果我想打开不存在的页面,应用程序会重定向我的 Error.aspx

但如果我没有在 url 中使用扩展程序(例如;*.aspx、*.html),应用程序将重定向默认错误页面。

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

如何解决这个问题?

谢谢。

要处理没有扩展名的 url,您需要在 web.config 文件中创建 <httpErrors>。这可以通过以下方式实现:

<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="ExecuteURL" >
<remove statusCode="403"/>
<remove statusCode="404"/>
<remove statusCode="500"/>
<error statusCode="403" responseMode="ExecuteURL" path="PathTo403ErrorPage" />
<error statusCode="404" responseMode="ExecuteURL" path="PathTo404ErrorPage" />
<error statusCode="500" responseMode="ExecuteURL" path="PathTo500ErrorPage" />
</httpErrors>
</system.webServer>