routes.MapRoute(
name: "Home",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Process",
url: "Process/{action}/{id}",
defaults: new { controller = "Process", action = "", id = UrlParameter.Optional }
);
1。当我点击http://localhost:7841/Process时,能帮助我理解为什么我收到HTTP 404错误 但是,当我点击时,我能够看到我的页面 http://localhost:7841/Process/list
此外,如果我在两个路线网址(见下文)中硬编码控制器(网址:“主页/ {动作} / {id}”),为什么我会“ HTTP错误403.14 - 禁止“错误。
routes.MapRoute(
name: "Home",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Process",
url: "Process/{action}/{id}",
defaults: new { controller = "Process", action = "", id = UrlParameter.Optional }
);
请帮助我理解路线。
答案 0 :(得分:1)
因为当您请求yourBaseUrl/Process/
时,它会匹配路由模式{controller}/{action}/{id}
,这是您定义的第一个路由的网址模式(名为Home
的路由模式) 。因此,它会尝试将请求发送到action方法,因为您在请求URL中没有action方法段,它将尝试使用在该路由注册中定义的默认值,即Index
。您收到的是404,因为您的Index
内没有ProcessController
操作方法。如果您向Index()
添加ProcessController
操作方法,它将执行该方法并从中返回结果。
理想情况下,您应该在通用路由定义之前定义所有特定路由定义。如果您希望/Process
返回List
方法返回的响应,请将其设置为路由注册中的默认操作。
routes.MapRoute(
name: "Process",
url: "Process/{action}/{id}",
defaults: new { controller = "Process", action = "List", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
另一种选择是使用RouteConfig
中的默认通用路由注册,并使用attribute routing使List
方法由/Process/
处理。< / p>
public class ProcessController : Controller
{
[System.Web.Mvc.Route("Process")]
public ActionResult List()
{
return Content("process list action method :)");
}
}