在mvc中使用RedirectToAction方法时遇到问题.我的地图路线看起来像这样.
I have an problem using the RedirectToAction method in mvc. My Map route look like this.
routes.MapRoute( "Project", // Route name "{Controller}/{Action}/{projectId}/{machineName}/{companyId}", new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 } // Parameter defaults);发送控制器看起来像这样:
And the sending Controller look like this:
[HttpPost] [ValidateInput(false)] public ActionResult Create(Project project, string text) { ViewBag.MachineType = new List<string>(new string[] { "Machine Type A", "Machine Type B", "Machine Type C", "Machine Type D", "Machine Type E" }); if (ModelState.IsValid) { UserAccess user = (UserAccess)(Session["UserAccessInfo"]); Int64 projectId = DataBase.DBProject.InsertProject(project.ProjectNumber, project.MachineName, project.MachineNameEnglish, 1, project.Serial, text, user.Company_Id,project.MachineType); return RedirectToAction ("Directives", "Project", new { projectId = 48, machineName = "Netduino", companyId = 27 }); // return RedirectToAction("Directives", "Project", new { projectId = projectId, machineName = project.MachineName, CompanyId = user.Company_Id }); } else { ModelState.AddModelError("", "Invalid username or password"); } return View(); }</pre>和回收控制器:
And the recivering controller:
public ActionResult Directives(int projectId, string machineName, int companyId) { convertedDirectives = new List<Models.Directives>(); ViewBag.MachineName = machineName; ViewBag.ProjectId = projectId; List<object> Directives = new List<object>(); if (ModelState.IsValid) { Directives = DataBase.DBProject.GetDirectives(companyId); foreach (List<object> dir in Directives) { Directives newDirective = new Models.Directives(); newDirective.CompanyID = Convert.ToInt32(dir[0]); newDirective.DirectiveId = Convert.ToInt32(dir[1]); newDirective.Id = Convert.ToInt32(dir[2]); newDirective.DirectiveName = Convert.ToString(dir[3]); newDirective.DirectiveNameShort = Convert.ToString(dir[4]); newDirective.Created = Convert.ToDateTime(dir[5]); convertedDirectives.Add(newDirective); } } else { ModelState.AddModelError("", "An error has "); } ViewBag.DirectivesList = convertedDirectives; return View(); }在地址栏中看起来像这样:
In the Adress bar it looks like this:
localhost:1843/Project/Directives?projectId=48&machineName=NetDuino&companyId=27但是我想要的方式就是这样.
But the way i want it is like this.
localhost:1843/Project/Directives/48/Netduino/27但是,如果尝试在manuel中键入url,则可以正常运行.我在做什么错?
But if trie to type the url in manuel it works prefect. What is im doing wrong?
推荐答案尝试RedirectToRoute.那应该适合您的情况.下面是一个简单的示例: Try RedirectToRoute. That should work for your case. A quick example is given below: routes.MapRoute("Project", // Route name "Route/Directives/{projectId}/{machineName}/{companyId}", new { controller = "Route", action = "Directives", projectId = @"\d+", machineName = @"\S+", companyId = @"\d+" });
请注意,我已经硬编码了控制器&操作,也可以使用您的方法并将控制器和操作作为路由值传递. 现在,在您的控制器中,而不是RedirectToAction,请使用RedirectToRoute,如Index action方法所示.
Notice that I have hard-coded the controller & action, you could also use your method and pass the controller and action as route values. Now in your controller, instead of RedirectToAction, use RedirectToRoute as shown in the Index action method.
public class RouteController : Controller { // // GET: /Route/ public ActionResult Index() { return RedirectToRoute("Project", new { projectId = 4, machineName = "somemachine", companyId = 7 }); } public ActionResult Directives(int projectId, string machineName, int companyId) { ViewData["Data"] = string.Format("{0},{1},{2}", projectId, machineName, companyId); return View(); } }现在,如果您导航到/route/index,它将把您重定向到/Route/Directives/4/somemachine/7,而不是您当前的方式. 希望这会有所帮助!
Now, if you navigate to /route/index it will redirect you to /Route/Directives/4/somemachine/7 instead of how you get currently. Hope this helps!
更多推荐
ASP.NET MVC3路由问题
发布评论