0

I have a controller action like this:

public ActionResult Index(string url)
{
     var pageTitle = url.Split('/')[0];
     var page = Services.PageService.GetPage(pageTitle);

     if (page == null)
     {
         throw new HttpException((Int32) HttpStatusCode.NotFound, "NotFound");
     }

      return View(page);
 }

Everytime I am debugging my site, when the HttpException is thrown I get a prompt from Visual Studio notifying me that the exception was unhandled by user code.

I guess I just want somebody to clarify that what I am doing is correct, and that this notification can be dismissed without worry. The event still bubbles up to the Application_Error method in my Global.asax file where I am actually handling HttpException's, so as far as I can tell the only problem is the inconvenience of VS telling me every time this exception is thrown.

Chris
  • 7,996
  • 11
  • 66
  • 98
  • Yup looks right to me, see http://www.tugberkugurlu.com/archive/asp-net-mvc---throwing-404-exceptions-manually-from-controller-when-model-is-null – undefined Feb 13 '12 at 09:40

2 Answers2

1

I guess I just want somebody to clarify that what I am doing is correct, and that this notification can be dismissed without worry.

Yes, what you are seeing is called a first chance exception. VS debugger notifies you of all exceptions occurring in your code. If you have a proper handler for this exception you should be fine.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
-1

I assume you want to return the http code (a 404 in this case) to the user/consumer of your page. So you should rather do this:

public ActionResult Index(string url)
{
     var pageTitle = url.Split('/')[0];
     var page = Services.PageService.GetPage(pageTitle);

     if (page == null)
     {
        return new HttpNotFoundResult();
     }
      return View(page);
 }

The HttpNotFoundResult is a new ActionResult in asp.net-mvc3, there is also a protected method HttpNotFound() as part of the controller class which does the same thing. In both cases you can also supply a message string.

Returning an ActionResult is also more graceful I think in terms of unit-testing.

gideon
  • 19,329
  • 11
  • 72
  • 113
  • HttpNotFoundResult doesn't render a view. It simply sets the status code to 404 and returns an empty result. See [this post](http://stackoverflow.com/questions/4985550/how-to-return-a-view-for-httpnotfound-in-asp-net-mvc-3) for details. – John Lieb-Bauman Sep 02 '14 at 15:53
  • @jbaum012 I'm not sure what exactly is wrong with the answer. The OP (about two years ago) was asking not to render a view but for a way to not have visual studio break. Even in the answer you linked it specifies the same thing, only additionally states that you must specify what html the 404 will return. Which is a separate thing in my opinion. The OP also just wanted to confirm his suspicion about the fact that the exception is a sanity check. Which is what I can tell from reading it now. – gideon Sep 02 '14 at 16:02
  • I dont mean to offend, but I just find your solution misleading. HttpNotFoundResult is not a proper alternative to throwing a new HttpException. Just as an example, if you were to specify a custom error page in your web.config, `return new HttpNotFoundResult` does **not** render your custom error page. – John Lieb-Bauman Sep 02 '14 at 16:22