“抛出新的HttpException”不会返回任何错误

时间:2014-02-21 16:05:07

标签: asp.net asp.net-mvc asp.net-mvc-4

我的操作方法中包含以下代码: -

public ActionResult ManageCustomerVLANs(string customerName)
        {
            if (!repository.IsCustomerExsists(customerName))
            {

               throw new HttpException(404, "Your error message");//RedirectTo NoFoundPage
            }

我在web.config中定义了以下内容,以处理任何404 http代码: -

<caching>
      <outputCacheSettings>
        <outputCacheProfiles>

          <add name="NoCache" noStore="true" duration="0" varyByParam="*"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
    <!--<customErrors mode="RemoteOnly"/>-->
    <customErrors mode="RemoteOnly">
      <error statusCode="404" redirect="~/Home/" />
    </customErrors>

但是目前如果action方法返回“throw new HttpException”,则实际上没有返回任何内容,并且在“throe new HttpException”之后执行将继续。 那么有人可以建议,我怎么能返回一个http 404?

1 个答案:

答案 0 :(得分:0)

你可以像这样返回404.

return new HttpStatusCodeResult(HttpStatusCode.NotFound);