我还没有做任何花哨的路由模式,只是基本的控制器、动作、id 样式。
但是,我的行为似乎从未通过 id。当我在任何一项操作中设置断点时,id 参数的值为 null。是什么赋予了?
全球.asax.cs:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Tenants", action = "Index", id = "" } // Defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
//RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory());
}
protected void Application_AuthenticateRequest()
{
if (User != null)
Membership.GetUser(true);
}
}
TenantsController.cs 上的 Index() 操作:
/// <summary>
/// Builds the Index view for Tenants
/// </summary>
/// <param name="tenantId">The id of a Tenant</param>
/// <returns>An ActionResult representing the Index view for Tenants</returns>
public ActionResult Index(int? tenantId)
{
//a single tenant instance, requested by id
//always returns a Tenant, even if its just a blank one
Tenant tenant = _TenantsRepository.GetTenant(tenantId);
//returns a list of TenantSummary
//gets every Tenant in the repository
List<TenantSummary> tenants = _TenantsRepository.TenantSummaries.ToList();
//bilds the ViewData to be returned with the View
CombinedTenantViewModel viewData = new CombinedTenantViewModel(tenant, tenants);
//return index View with ViewData
return View(viewData);
}
tenantId 参数的值为aways null!!!啊!愚蠢的是,当我使用 Phil Haack 的 Route Debugger 时,我可以清楚地看到调试器看到了 id。什么废话?!