asp.net mvc 3将“URL”从“/”更改为“?id =”

时间:2011-05-31 16:29:17

标签: asp.net-mvc asp.net-mvc-3

我正在使用MVC 3.页面内容来自SQL数据库。当我尝试使用Javascript传递变量时,我注意到一个问题。当url显示目录/ pagename时它不起作用,但是如果url是目录?= pagename !!它可以工作!知道为什么以及如何更改网址?= id。 这是行动链接:

<%: Html.ActionLink("Go", "CourseContent", new { id = Model.ID })%>

全球路由

public static void RegisterRoutes(RouteCollection routes)
{
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );
    }

这是控制器:

public virtual ActionResult CourseContent(long id)
{
   Course requestedCourse = _courseSvc.GetCourse(id);
   if (requestedCourse == null)
       return new HttpNotFoundResult();

    string loggedInUserName = HttpContext.User.Identity.Name;
    string loggedInRoleName = _membershipSvc.GetRoleForUser(loggedInUserName);

    if (_courseSvc.DetermineIfTheUserCanAttendCourse(id, Request.IsAuthenticated, loggedInUserName, loggedInRoleName))
       return View(requestedCourse);

    return RedirectToAction("Index");
}

提前致谢

1 个答案:

答案 0 :(得分:0)

它应该与/ controller / action / id一起使用,但你总是可以这样做:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}?id={id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  );
}