书呆子晚餐控制器
在此页面上:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ASP.NET MVC 更倾向于约定而不是配置。 这意味着它将查找带有 Controller 后缀的控制器,并且不将其作为 URL 的一部分,而仅包含 Controller 的前缀。 因此,如果您有 HomeController,您可以访问 /Home/,就像 DiningsController 意味着 /Dinners/ 一样。 这是 ASP.NET MVC 框架本身的一部分。
如果您查看 Global.asax 中的默认路由,您会发现它使用的 URL 格式类似于...
这意味着获取控制器的名称和操作的名称,并将请求指向该方法。
因此,对于 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...
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.
我面前没有示例,但我怀疑路由(参见 Global.asax)被设置为使用
Dinners
作为默认控制器名称; 负责的具体行看起来像编辑:重新阅读问题,这并不能完全回答它; 您感到困惑的问题是
/dinners
已解析为DinnersController
; 没有任何默认行为。 对于这种情况,查德的答案是正确的,正如您所说 - 框架基本上砍掉了控制器部分并计算出要映射的名称。I don't have the sample in front of me, but I suspect the routing (see
Global.asax
) is set up to useDinners
as the default controller name; the specific line responsible would look something likeEdit: Re-reading the question, this doesn't quite answer it; the issue you were confused over is that
/dinners
was resolved to theDinnersController
; not any default behaviour. For that case, Chad's answer is right, and as you said - the framework basically chops off theController
part and works out what name to map.这是 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).