我還沒有做任何奇特的路線模式,只是基本的控制器,動作,身份證樣式。asp.net mvc id不從路由中拉出?
但是,我的行爲似乎沒有通過id。當我在任何一個動作中插入一個斷點時,id參數的值爲null。是什麼賦予了?
的Global.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行動:
/// <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參數的值是贈品空!哎呀!愚蠢的是,當我使用Phil Haack的Route Debugger時,我可以清楚地看到調試器看到了這個id。什麼廢話?!
你和Stephen的回答幾乎完全相同,你的答案都是正確的。然而,接受MrDustpan先生是因爲他早1分鐘。斯蒂芬很好的解釋。 – NovaJoe