NerdDinner 异常和自定义错误

发布于 2024-08-22 09:10:44 字数 1043 浏览 0 评论 0原文

教程中的详细信息操作中,Scott 使用

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

返回 404 Not Found 消息视图。

但在我下载的 NerdDinner 源代码中,有以下几行:

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

这将转到 FileNotFoundResult,其中有:

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

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

就是这样。从这里开始如何引用 NotFound.aspx 视图?我无法找出它是如何映射到 NotFound.aspx 的,尽管 NotFound.aspx 确实存在于晚餐视图文件夹中。 web.config 中也没有任何内容。

上面的代码来自 Change Set 41262,而不是 1.0 版本。

问题(更清楚一点):怎么会“抛出 new HttpException(404 ,消息)”返回NotFound视图?

请有人解释一下。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮色兮凉城 2024-08-29 09:10:44

当抛出 HttpException 时,它最终会被 asp.net 运行时捕获,并通过提供错误页面来处理它。可以在 customErrors webConfig 选项中自定义此错误页面

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

您确定它仍在加载 NotFound.aspx 页面,而不是 Confused.aspx 吗?

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

而不是

"Are you lost? Try taking a look at the complete list of ..."

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 ..."
只是我以为 2024-08-29 09:10:44

当我下载它时,Views\Dinners 文件夹中有一个 NotFound.aspx 视图。

更新:就像 womp 所说的那样,提出一个不清楚的问题然后否决答案是非常不诚实的。我怀疑您澄清的问题的答案是下载的教程使用 FileNotFoundResult 异常作为占位符,并且它应该被 NotFound 视图替换。我对我的 NerdDinner 解决方案进行了快速搜索,这是我在完成完整教程后留下的内容,并且 FileNotFoundResult 没有在任何地方使用。

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.

挥剑断情 2024-08-29 09:10:44

这是 ASP.Net MVC 框架的约定之一。我强烈建议阅读一些有关框架约定的 ASP.Net MVC 教程和文档。 这是关于视图的

基本上,当您调用控制器的 View() 方法时,这是显式在视图模板文件的路径中进行编码的快捷方式。约定是框架将首先在控制器的 View 目录中查找视图模板,然后在 Shared 目录中查找。它还足够智能,可以在搜索模板时同时查找 .aspx 和 .ascx 扩展名。

这就是为什么如果您想要加载不在这两个文件夹中的视图,则必须更加明确。例如,要加载不在当前控制器的 View 文件夹中的部分视图,您必须指定它的完整路径:

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

而不是通常的

<% Html.RenderPartial("SomeView") %>

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") %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文