Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently my page url is like that
www.something.com/Controller/ActionName/Id
but what i need is when someone types this url it should redirect to www.something.com/Controller/ActionName/Id/Name

what currently I am doing is

C#
[HttpGet]
public ActionResult Details(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Project project = new Project();
            project = GetProjectFromDatabase(Id);
            string name = project.Name;
            name = name.Replace(" ", "-");

            return Redirect("/Project/Details/"+id+"/"+project+"/"+name);//Problem at this line
i want here to redirect to second action
        }
     //and there is another controller with same name 
public ActionResult Details(int id, Project project, string name = "")
        {
            return View("~/Views/Project/_Details.cshtml", project);
        }
 but when i am redirecting to another controller with same name, it redirects to itself 

Can someone help me
-Thanks
Posted
Comments
Nathan Minier 19-Sep-14 8:24am    
I'm guessing it's because your RouteConfig looks like "{controller}/{action}/{id}", so it is ignoring the project and name terms.
You could try RedirectToAction("Details",/*Controller*/, new { project = project });
(You can get name from project.name, making that assignment redundant)

You can also store variables in TempData if you are using redirects, which is how I would suggest proceeding. The tempdata will last for exactly one redirect.

TempData["Project"] = GetProjectfromDatabase(Id);
return RedirectToAction("Details", /*Controller*/);
Vishal Pand3y 22-Sep-14 7:28am    
problem of lazy loading with temp data
Nathan Minier 22-Sep-14 8:06am    
You should only have that issue if you haven't forced the IQueryable to process yet, so a .ToList(), .First() or .Single() should fix that.

1 solution

place it like this :-
C#
"/Project/Details/"+id+"/"+project+"/?name="+name


This is not the default value as Id is, so route config would be expecting a value there.
else try :
C#
return RedirectToAction( action = "Details",controller = controllerName, new{id=id,project=project,name=name})
 
Share this answer
 
v2
Comments
Nathan Minier 19-Sep-14 9:37am    
http://msdn.microsoft.com/en-us/library/dd505283%28v=vs.118%29.aspx

Check your parameters against the documentation please.
[no name] 19-Sep-14 9:50am    
thanks sorry for that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900