gpt4 book ai didi

ASP.NET MVC 2 urls/routes, View 如何与 Controller 相关?

转载 作者:行者123 更新时间:2023-12-04 22:17:13 25 4
gpt4 key购买 nike

有人可以解释路由如何与 MVC 2 中的 Controller 相关联吗?目前,我在/Controllers/HomeController.cs 中有一个 Controller 和一个 View /Home/Index.aspx。

我的路线注册方法如下所示:

 public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with parameters
new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
);
}

如果我请求 URL: http://localhost/Home/Index ,则请求由 HomeController.Index() 正确处理。

但是,在我的一生中,我无法弄清楚 url/Home/Index 是如何指向 HomeController 的。据我所知, View aspx 没有引用 HomeController,HomeController 没有引用 View ,路由表也没有明确提及 HomeController。这是如何神奇地发生的?当然,我错过了一些明显的东西。

然后

最佳答案

ASP.NET MVC 附带的默认 View 引擎基于以下约定工作:
你有一个这样的文件夹结构:

- Controllers\
|- HomeController.cs
- Views\
|- Home\
|-- Index.aspx
|- Shared\
当一个请求进入并匹配 RegisterRoutes 方法中定义的路由时(有关更多信息,请参阅 URL routing 之类的内容),然后调用匹配的 Controller :
routes.MapRoute(
"Default", // Route name, allows you to call this route elsewhere
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
在默认路由中,您还指定了一个默认 Controller (没有“Controller”后缀)——路由引擎会自动为你添加 Controller 到 Controller 名称上——以及一个默认操作。
在您的 Controller 中,您调用简单方法:
public ActionResult Index(){
return View();
}
然后,默认 View 引擎在“Views”文件夹(约定)中名为“Home”(与 Controller 相同)的文件夹中查找名为 Index(与操作相同)的 aspx 文件。
如果在那里没有找到,它还会在共享文件夹中查找索引页。
来自 ASP.NET MVC Nerd Dinner sample chapter

ASP.NET MVC applications by default use a convention-based directory naming structure when resolving view templates. This allows developers to avoid having to fully-qualify a location path when referencing views from within a Controller class. By default ASP.NET MVC will look for the view template file within the \Views\[ControllerName]\ directory underneath the application.

The \Views\Shared subdirectory provides a way to store view templates that are re-used across multiple controllers within the application. When ASP.NET MVC attempts to resolve a view template, it will first check within the \Views\[Controller] specific directory, and if it can’t find the view template there it will look within the \Views\Shared directory.

When it comes to naming individual view templates, the recommended guidance is to have the view template share the same name as the action method that caused it to render. For example, above our "Index" action method is using the "Index" view to render the view result, and the "Details" action method is using the "Details" view to render its results. This makes it easy to quickly see which template is associated with each action.

Developers do not need to explicitly specify the view template name when the view template has the same name as the action method being invoked on the controller. We can instead just pass the model object to the View() helper method (without specifying the view name), and ASP.NET MVC will automatically infer that we want to use the \Views\[ControllerName]\[ActionName] view template on disk to render it.



编辑添加:
我设置的一些示例路由,明确设置 Controller 是:
routes.MapRoute(
"PhotoDetailsSlug",
"Albums/{albumId}/{photoId}/{slug}",
new {controller = "Albums", action = "PhotoDetails"},
new {albumId = @"\d{1,4}", photoId = @"\d{1,8}"}
);
在这里,我明确声明我正在使用相册 Controller 和 PhotoDetails 操作,并将各种 ID 等传递给该操作。

关于ASP.NET MVC 2 urls/routes, View 如何与 Controller 相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1595971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com