书呆子晚餐控制器

发布于 2024-07-20 06:04:31 字数 1573 浏览 8 评论 0原文

在此页面上:

http://nerddinnerbook.s3.amazonaws.com/Part4.htm

添加控制器后,我可以浏览到 http://localhost:xxxx/dinners 并且它按预期工作。 我的问题是它怎么知道使用“晚餐”? “晚餐”位于哪里? 我的控制器名为dinnerscontroller,那么dinners这个词是如何变得有意义的呢? 我在 Linq to SQL 或其他任何地方都没有看到它。 我确信我忽略了一些显而易见的事情。

这是代码:

<前> <代码> // // HTTP-GET: /晚餐/ 公共无效索引() { Response.Write("

即将推出:

晚餐"); }

<前> <代码> // // HTTP-GET:/晚餐/详细信息/2 公共无效详细信息(int id) { Response.Write("

详细晚餐ID:

" + id + ""); }

“晚餐”从哪里来

感谢您的任何帮助。

编辑:我在发布之前进一步阅读了文章并看到了 global.asax,但我不明白它是如何映射到晚餐的:

 public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }

On this page:

http://nerddinnerbook.s3.amazonaws.com/Part4.htm

After the controller is added, I can browse to http://localhost:xxxx/dinners and it works as expected. My question is how does it know to use "Dinners"? Where is "Dinners" located? My controller is named DinnersController so how did the word Dinners become meaningful. I don't see it in my Linq to SQL or anywhere else. I'm sure I'm overlooking something obvious.

Here is the code:

    //
        // HTTP-GET: /Dinners/

        public void Index()
        {
            Response.Write("<h1>Coming Soon:

Dinners");
}

        //
        // HTTP-GET: /Dinners/Details/2

        public void Details(int id)
        {
            Response.Write("<h1>Details DinnerID:

" + id + "");
}

Where is "Dinners" coming from?

Thank you for any help.

EDIT: I read further in the article before I posted and saw about the global.asax, but I don't understand how it mapped to dinners with this:

 public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }

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

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

发布评论

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

评论(3

终弃我 2024-07-27 06:04:31

ASP.NET MVC 更倾向于约定而不是配置。 这意味着它将查找带有 Controller 后缀的控制器,并且不将其作为 URL 的一部分,而仅包含 Controller 的前缀。 因此,如果您有 HomeController,您可以访问 /Home/,就像 DiningsController 意味着 /Dinners/ 一样。 这是 ASP.NET MVC 框架本身的一部分。

如果您查看 Global.asax 中的默认路由,您会发现它使用的 URL 格式类似于...

"{controller}/{action}/{id}"

这意味着获取控制器的名称和操作的名称,并将请求指向该方法。

因此,对于 DiningsController Index 操作方法,它看起来像 /Dinners/Index。

ASP.NET MVC favors Convention over Configuration. Meaning it will look for a controller with a Controller suffix and not include it as part of the URL and only include the prefix to Controller. So if you have HomeController you could visit /Home/ just as DinnersController means /Dinners/. This happens as part of the ASP.NET MVC framework itself.

If you look at the default route in Global.asax you'll see it uses a format for the URL that looks like...

"{controller}/{action}/{id}"

This means take the name of the controller and the name of the action and point the request to that method.

So for DinnersController Index action method it would look like /Dinners/Index.

咋地 2024-07-27 06:04:31

我面前没有示例,但我怀疑路由(参见 Global.asax)被设置为使用 Dinners 作为默认控制器名称; 负责的具体行看起来像

new { controller = "Dinners", action = "Index", id = "" }

编辑:重新阅读问题,这并不能完全回答它; 您感到困惑的问题是 /dinners 已解析为 DinnersController; 没有任何默认行为。 对于这种情况,查德的答案是正确的,正如您所说 - 框架基本上砍掉了控制器部分并计算出要映射的名称。

I don't have the sample in front of me, but I suspect the routing (see Global.asax) is set up to use Dinners as the default controller name; the specific line responsible would look something like

new { controller = "Dinners", action = "Index", id = "" }

Edit: Re-reading the question, this doesn't quite answer it; the issue you were confused over is that /dinners was resolved to the DinnersController; not any default behaviour. For that case, Chad's answer is right, and as you said - the framework basically chops off the Controller part and works out what name to map.

┈┾☆殇 2024-07-27 06:04:31

这是 MVC 通过其默认路由为您提供的“隐式配置”(约定与配置)的一部分。

当您访问“www.mysite.com/dinners”时,MVC 将自动查看 URL 中的“dinners”,并查找名为“DinnersController”的控制器并调用“Index”方法。 如果您访问“www.mysite.com/dinners/details”,MVC 将查找“DinnersController”并调用“Details”方法。

如果您查看 ruby​​ on Rails,您会发现它的行为方式大致相同(几乎可以肯定,这就是 Microsoft 的来源)。

This is part of the "implicit configuration" (Convention vs. Configuration) that MVC gives you via its default routes.

When you go to "www.mysite.com/dinners" MVC will automatically see the "dinners" in the URL and will look for a controller named "DinnersController" and call the "Index" method. If you went to "www.mysite.com/dinners/details", MVC would look for the "DinnersController" and call the "Details" method.

If you look at ruby on rails, it behaves in much the same way (which is almost certainly where Microsoft got this from).

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