Kin*_*sin 5 asp.net asp.net-mvc routes url-routing asp.net-mvc-routing
我怎样才能告诉我的mvc-application 路由到特定的Controller以及Action何时未指定?
调试http://localhost:54500/时应路由到http://localhost:54500/Home/Index.
目前我有:
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
但这总是抛出
未找到视图“索引”或其主视图或没有视图引擎支持搜索的位置
编辑 #1
它应该重定向/路由到驻留在Area被调用的视图中Home。只是想澄清一下,有 aController和 anArea两者都被命名为Home。
该区域的配置是:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
Run Code Online (Sandbox Code Playgroud)
调试时http://localhost:54500/应该路由到http://localhost:54500/Home/Index。
实际上,按照您的配置方式,http://localhost:54500/将路由到该HomeController.Index方法,而不是另一个 URL。
未找到视图“索引”或其主视图或没有视图引擎支持搜索的位置
此错误表示路由成功,但控制器返回了不存在的视图的路径。
由于您还提到您正在使用区域并发布了您的配置,因此很清楚发生了什么。您的配置按以下顺序运行:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
因此,如果您传递 URL http://localhost:54500/,则 Area 路由将丢失(因为它不以 开头/Home)并且它将匹配该Root路由。此Root路线不会路由到您的区域。有 2 种方法可以解决此问题。
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index" }
);
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
).DataTokens["area"] = "Home";
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8046 次 |
| 最近记录: |