默认路由不起作用 [英] Default route not working
问题描述
为什么不起作用?
路线:
routes.MapRoute(
"Summary",
"{controller}/{id}",
new { controller = "Summary", action = "Default" }
);
控制器:
public class SummaryController : Controller
{
public ActionResult Default(int id)
{
Summary summary = GetSummaryById(id);
return View("Summary", summary);
}
}
URL:
http://localhost:40353/Summary/107
错误:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Summary/107
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225
更新:
让我用一个更聪明的问题来更新问题。我怎么能同时拥有这两个?
Let me update the question with a more intelligent one. How can I have both of these?
routes.MapRoute(
"Home",
"{controller}",
new { controller = "Home", action = "Default" }
);
routes.MapRoute(
"Summary",
"{controller}/{id}",
new { controller = "Summary", action = "Default" }
);
推荐答案
路由如何工作(默认情况下)?
让我们回到默认路线,有点像这样:
How do routes work (by default)?
Let's get back to the default route, which is somewhat like this one:
routes.MapRoute(
// Route name
"Default",
// URL with parameters
"{controller}/{action}/{id}",
// Parameter defaults
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
让我们尝试了解它的工作原理。
Let's try to understand how this one works.
-
如果您访问
/
,它将调用Index
操作Home
控制器的;
If you access
/
, it will call theIndex
action of theHome
controller; the optional Id was ommitted.
如果您访问 / C
,它将调用<$ c $ C
控制器的c> Index 操作;
If you access /C
it will call the Index
action of the C
controller; the optional Id was ommitted.
如果您访问 / C / A
,则会调用<$ C
控制器的c $ c> A 动作;
If you access /C/A
it will call the A
action of the C
controller; the optional Id was ommitted.
如果您访问 / C / A / 1
,它将调用 C
控制器的ID为 1
的 A
动作。 / p>
If you access /C/A/1
it will call the A
action of the C
controller with id 1
.
因此,该路由允许任何形式为 /
的URL , / C
, / C / A
和 / C / A / 1
,其中 C
是控制器,而 A
是动作。这是什么意思?这意味着您不必必须指定自己的路线。
So, that route allows any URLs of the form /
, /C
, /C/A
and /C/A/1
where C
is a controller and A
is an action. What does this mean? This means that you don't necessarily have to specify your own routes.
因此,没有路线,您可能只需要 HomeController
和 SummaryController
,然后向最后一个名为 Show
的控制器添加一个操作。
So, without routes you could just have a HomeController
and a SummaryController
and add an action to that last controller called Show
.
然后 / Summary / Show / 1
将调用 SummaryController.Show(1)
让我们假设要映射这样的路由,使得 / Summary / 1
调用 SummaryController.Show(1)
。
Let's suppose we do want to map routes such that /Summary/1
calls SummaryController.Show(1)
.
以下是正确的格式:
routes.MapRoute(
"Summary",
"Summary/{id}",
new { controller = "Summary", action = "Show" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
请注意,我们已更改了 Home
路线看起来像默认
路线。现在,我们添加了 Summary
路由,该路由告诉我们 Summary / {id}
形式的URL路线。当他们这样做时,它将调用 Summary
控制器的 Show
动作,并传递 id
作为参数;正是您想要的...
Note that we have changed the Home
route to look like the Default
route. Now we've added a Summary
route where we tell that URLs of the form Summary/{id}
will trigger that route. When they do, it calls the Show
action of the Summary
controller and pass along id
as a parameter; which is exactly what you want...
还请注意,我们需要先放置 Summary
路线,以便
Also note that we need to place the Summary
route first such that it gets precedence.
注意::您将不想为创建的每个动作创建新的路线。您也不希望所有动作都在同一控制器中。如果是其中一种情况,请考虑重新考虑您的方法,以免以后再遇到问题...
Caution: You will not want to create a new route for every single action you create. You also don't want all your actions to be in the same controller. Consider rethinking your approach if one of these is the case, so that you don't end up with problems later...
这篇关于默认路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!