ASP.NET MVC3路由问题 [英] Asp.net mvc3 Routing problem

查看:75
本文介绍了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:

http://localhost:1843/Project/Directives?projectId=48&machineName=NetDuino&companyId=27



但是我想要的方式就是这样.



But the way i want it is like this.

http://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路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆