ASP.NET MVC3路由问题

编程入门 行业动态 更新时间:2024-10-27 10:24:43
本文介绍了ASP.NET MVC3路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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路由问题

本文发布于:2023-11-12 13:47:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   ASP   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!