0

I have the following code inside my action methods:-

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

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

And I have defined the following inside my web.config, to handle any 404 http code:-

<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>

But currently if the action method return “throw new HttpException”, nothing is actually returned and the execution will continue after this “throe new HttpException”. so can anyone advice, how I can return an http 404 ?

John John
  • 1
  • 72
  • 238
  • 501
  • its better to return `HttpStatusCodeResult` instead as shown here http://stackoverflow.com/questions/17148554/asp-net-mvc-4-throw-httpexception-vs-return-httpstatuscoderesult?rq=1 – Amitd Feb 21 '14 at 16:13
  • or you can try the way shown here http://stackoverflow.com/questions/13905164/how-to-make-custom-error-pages-work-in-asp-net-mvc-4?rq=1 – Amitd Feb 21 '14 at 16:14

1 Answers1

0

You can return 404 like this.

return new HttpStatusCodeResult(HttpStatusCode.NotFound);
Ananthan Unni
  • 1,304
  • 9
  • 23