1. Default.aspx页面代码如下:
public void Page_Load(object sender, System.EventArgs e)
{
// Change the current path so that the Routing handler can correctly interpret
// the request, then restore the original path so that the OutputCache module
// can correctly process the response (if caching is enabled).
string originalPath = Request.Path;
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
HttpContext.Current.RewritePath(originalPath, false);
}
请求被重写为“/”,从而使设置的起始页路径被重定向
2.不解析Default.aspx(Web)页面
RouteTable.Routes.RouteExistingFiles = false;如果找不到相应的控制器,则页面会报错,正如将这里的值改为true。
3. 改为下述代码则不会报错,因为指定了页面的路由解析是正确的。路由按顺序解析,如果解析错误就会报错
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
RouteTable.Routes.RouteExistingFiles = true;
// RouteMonitor.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Start",
"Default.aspx",
new { controller = "Home", action = "Index", id = "" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
}
4. 添加 RouteMonitor.dll文件,用于监测路由表信息,该文件在资源文件中存在,代码如下:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
RouteTable.Routes.RouteExistingFiles = true;
RouteMonitor.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
/* routes.MapRoute(
"Start",
"Default.aspx",
new { controller = "Home", action = "Index", id = "" }
);
*/
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
}
5. 路由解析执行顺序
用户请求(UrlRouting)
路由检索(RouteTable)
匹配路由
路由解析(Route)
路由处理程序(IRouteHandler)
HTTP处理程序(IHttpHandler)
响应请求