ASP.NET MVC 路由返回 404 而没有任何操作
我正在使用 MVC2 Preview 1 开发一个非常简单的应用程序。
我有一个名为 ContentController 的控制器。我的问题是 /Content/Index 工作正常,但 /Content/ 返回 404。我正在 Studio 开发服务器上运行该应用程序。
使用 RouteDebugger 进行测试,但 /Content/ 返回 404,并且不显示任何调试信息。
我没有更改路由代码:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
这是我的控制器:
public class ContentController : Controller
{
IRepository _repo = new SimpleRepository("db", SimpleRepositoryOptions.RunMigrations);
public ActionResult Index()
{
var content = _repo.GetPaged<Content>(0, 20);
return View(content);
}
I am working on a very simple application, using MVC2 Preview 1.
I have a controller named ContentController. My problem is that /Content/Index works correctly, but /Content/ returns a 404. I am running the application on the Studio Development Server.
Tested with RouteDebugger but /Content/ returns a 404, and does not display any debugging information.
I have not changed the routing code:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
This is my controller:
public class ContentController : Controller
{
IRepository _repo = new SimpleRepository("db", SimpleRepositoryOptions.RunMigrations);
public ActionResult Index()
{
var content = _repo.GetPaged<Content>(0, 20);
return View(content);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个盲目的尝试,但是您还有一个名为 /Content/ 的目录吗?
It's a shot in the dark, but do you have a directory named /Content/ as well?
/Content 是一个控制器,它基本上只是操作的集合。 ASP.NET MVC 需要知道您想要运行哪个操作,因此通过省略该操作,asp.net mvc 不知道要返回哪个操作并给出 404。
您可以通过添加路由来告诉它默认值:
例如:
属性定义如下:
'ContentDefault`:路由名称(在路由表中必须是唯一的)
内容: URL 段(尝试将其更改为 ' Content/Much/Longer/URL',然后转到 http://localhost/Content/Much/Longer/ URL 查看其工作原理)
new {controller=.., action=...}:为此路由运行哪个控制器/操作组合。
您还可以在控制器中重写 HandleUnknownAction:
哦,顺便提一下,关于路由的额外建议......如果您在大括号 { } 中向路由添加某些内容,这些内容将作为属性传递给操作。
例如 /Content/Much/Longer/Url/{page}
所以 URL http://localhost/ Content/Much/Longer/Url/999
会将 999 传递到您的操作中,作为页面属性
我喜欢 MVC - 永远不会回到 WebForms - 这就是 Web 开发应该如何!
/Content is a controller, which is basically just a collection of actions. ASP.NET MVC needs to know WHICH action you want to run, so by leaving out the action asp.net mvc doesn't know what action to return and gives a 404.
You can tell it a default either by adding a route:
eg:
The attributes are defined as follows:
'ContentDefault`: Name of the Route (must be unique in your routing table)
Content: The URL segment (try changing this to 'Content/Much/Longer/URL' and then go to http://localhost/Content/Much/Longer/URL to see how this works)
new {controller=.., action=...}: which controller/action combo to run for this route.
You could also override HandleUnknownAction in your controller:
Oh and incidentally, an extra piece of advice about routing.... if you add something to the route in braces { } these will be passed to the action as an attribute.
e.g. /Content/Much/Longer/Url/{page}
so the URL http://localhost/Content/Much/Longer/Url/999
will pass the 999 into your action, as the page attribute
I love MVC - never going back to WebForms - this is how web development should be!