当前位置: 代码迷 >> ASP.NET >> asp.net mvc3 路由有关问题求解
  详细解决方案

asp.net mvc3 路由有关问题求解

热度:4600   发布时间:2013-02-25 00:00:00.0
asp.net mvc3 路由问题求解
Global.asax中代码:
C# code
   public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                "Default", // 路由名称                "{controller}/{action}/{id}", // 带有参数的 URL                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值            );            routes.MapRoute(             "Paging", // 路由名称             "{controller}/{action}/{Tid}/{page}", // 带有参数的 URL             new { controller = "Home", action = "List", Tid = UrlParameter.Optional, page = UrlParameter.Optional } // 参数默认值                   );              }

控制器代码:
C# code
    public class HomeController : Controller    {        public ActionResult Index()        {            ViewBag.Message = "欢迎使用 ASP.NET MVC!";            return View();        }        public ActionResult About()        {            return View();        }        public ActionResult List(string Tid,string page)        {            string s = string.Format("Tid:{0},page:{1}",Tid,page);            return View(s);        }    }

View代码:/Views/Home/List.cshtml
C# code
@{    ViewBag.Title = "List";}<h2>List</h2>

提示信息:
未找到视图“Tid:2,page:3”或其母版视图,或没有视图引擎支持搜索的位置。搜索了以下位置:
~/Views/home/Tid:2,page:3.aspx
~/Views/home/Tid:2,page:3.ascx
~/Views/Shared/Tid:2,page:3.aspx
~/Views/Shared/Tid:2,page:3.ascx
~/Views/home/Tid:2,page:3.cshtml
~/Views/home/Tid:2,page:3.vbhtml
~/Views/Shared/Tid:2,page:3.cshtml
~/Views/Shared/Tid:2,page:3.vbhtml
哪位大侠能帮忙解决一下 结贴加分,帮顶给分


------解决方案--------------------------------------------------------
url跳转时应该为:/Views/Home/2/3 这样的形式

view里取Tid 时用 Html.ViewContext.RouteData.Values["Tid"]
------解决方案--------------------------------------------------------
路由配置写成 传参的形式,并且直接启动控制器,通过控制器传输给view
------解决方案--------------------------------------------------------
controller里面接受参数的写法有问题
  相关解决方案