我正在学习MVC,目前正在研究路由。
我有以下问题:这是我的RegisterRoutes方法:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Customer", "{controller}/{action}/{objCustomer}",
new {controller = "Customer", action = "CreateCustomer", id = UrlParameter.Optional});
}
如果我运行我的应用程序, hxxp:// localhost:12454 / 是否应该在CustomerController中显示由CreateCustomer操作调用的视图,换句话说,URL应该是这样的? hxxp://本地主机:12454 /客户/ CreateCustomer
注意:我用hxxp替换了http,不尝试创建链接
我在这里没有正确理解的是什么?
这是我的整个Global.asax.cs类。
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Customer", "{controller}/{action}",
new { controller = "Customer", action = "CreateCustomer", UrlParameter.Optional});
}
}
这是我的CustomerController:
public class CustomerController : Controller
{
// GET: Customer
public ActionResult ShowCustomer(Customer objCustomer)
{
return View(objCustomer);
}
public ActionResult CreateCustomer()
{
return View();
}
}
答案 0 :(得分:1)
您在路由中使用'id'
而不是objCustomer
,而是必须将objCustomer
指定为可选路由参数。
修改路线,如下所示:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Customer", "{controller}/{action}/{objCustomer}",
new {controller = "Customer", action = "CreateCustomer", objCustomer = UrlParameter.Optional});
}
在appStart文件夹中的routeconfig.cs文件中创建所有自定义路由,并且不要忘记将此自定义路由置于默认路由之上。