views:

224

answers:

4

In this tutorial in Details action Scott uses

if (dinner == null) 
return View("NotFound");        
else
return View("Details", dinner); 

to return 404 Not Found message view.

But in my downloaded source code for NerdDinner there are these lines:

        if (dinner == null) {
            return new FileNotFoundResult { Message = "No Dinner found for that id" };
        }

This goes to FileNotFoundResult where there is this:

public class FileNotFoundResult : ActionResult
{
    public string Message { 
        get; 
        set; 
    }

    public override void ExecuteResult(ControllerContext context) {
        throw new HttpException(404, Message);
    }
}

And that's it. How is the reference to NotFound.aspx view made from here on? I was unable to found out how this gets mapped to NotFound.aspx, though NotFound.aspx does exist in Dinners view folder. There's also nothing in web.config..

The above code is from Change Set 41262 not the 1.0 version.

Question (to be more clear about it): How come "throw new HttpException(404, message)" returns NotFound view??

Someone please explain.

A: 

Did you test the downloaded code to see if the not found page is returned?

I know from experience this line of code explicitly returns the view NotFound.aspx

return View("NotFound");

While I can guess that the code you have downloaded returns an actual error to the browser, I am sure it is explained later on in his book.

Jimmy
Well that's obvious..Of course this line returns NotFound view because it is explicitly written as a parameter to ViewResult. But how does "throw new HttpException(404, message)" return NotFound view??
mare
A: 

There was a NotFound.aspx view in the Views\Dinners folder when I downloaded it.

Update: Like womp says it's very disingenuous to ask an unclear question and then downvote answers. I suspect the answer to your clarified question is that the downloaded tutorial uses the FileNotFoundResult exception as a placeholder and it's meant to be replaced by the NotFound view. I did a quick search on my NerdDinner solution, which is what I was left with after going through the complete tutorial, and the FileNotFoundResult is not used anywhere.

sipwiz
yeah as I said, there is NotFound.aspx but I don't know how the connection/reference is made..
mare
Please download the latest code (41262) and you should find it there. The way with FileNotFoundResult custom ActionResult enables us to return HTTP Status codes with the View, in this case 404. Version 1.0 doesn't do that.
mare
A: 

This is one of the conventions of the ASP.Net MVC framework. I highly suggest reading some of the ASP.Net MVC tutorials and documentation regarding the conventions of the framework. Here's the one on Views.

Basically, when you call the View() method of a controller, it's a shortcut for explicitly having to code in the path to a view template file. The convention is that the framework will look for the view template first in the controller's View directory, and then in the Shared directory. It's also smart enough to look for both .aspx and .ascx extensions when it goes searching for the template.

This is why if you want to load a View that's not in either of those folders, you have to be much more explicit. For example, to load a partial view that's not in the current controller's View folder, you have to specify the whole path to it:

<% Html.RenderPartial("~/Views/SomeOtherController/SomeView.ascx") %>

rather than the usual

<% Html.RenderPartial("SomeView") %>
womp
that is not at all what he is asking about
Jimmy
Damn so many and so fast answers and all wrong..
mare
With so many wrong answers, perhaps you need to clarify your question. As it is, the downvotes have pretty much stopped any motivation for me to further try to understand and help you.
womp
I'm working with MVC for 6 months now, you don't have to explain the obvious. And instructing me to go to tutorials which have nothing to do with it. Its answering for the sake of answering...BTW I have edited my question now
mare
Right. Ok good luck then.
womp
A: 

When an HttpException is thrown, it ends up being caught by the asp.net runtime, which will handle it by serving an error page. This error page can be customized in the customErrors webConfig option

<customErrors mode="RemoteOnly" defaultRedirect="/Dinners/Trouble">
            <error statusCode="404" redirect="/Dinners/Confused"/>

Are you sure it's still loading the NotFound.aspx page, and not the Confused.aspx?

"Sorry - but the dinner you requested doesn't exist or was deleted."

and not

"Are you lost? Try taking a look at the complete list of ..."
Tanzelax
This is from NotFound.aspx:" <h2>Lost?</h2> <p>Sorry - but the dinner you requested doesn't exist or was deleted.</p>"This is from Confused.aspx:" <h2>Confused?</h2> <p>Are you lost? Try taking a look at the complete list of <a href="/Dinners">Upcoming Dinners</a>.</p>"So no, it is rendering NotFound.aspx that's why I am asking because it is not in Web.config or anywhere else..
mare
http://www.nerddinner.com/1604698798
mare