Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,

I am using MVC4, and I am new to it. I need to determine one route that takes any action from a same controller redirects to a single/same action. I didnt found sufficient information.


eg.

I have a Home controller and it contains several actions. one of them is "Content". What I need is like any action called redirect to Content action only, the url remains with the same as older action.
Posted

Hi Amolpatil,

You can do it in two ways,

1) Using redirect to action
SQL
[HttpPost]
public ActionResult Index(string Name)
{
 ViewBag.Message = "Hi, Dot Net Tricks";
 //Like Response.Redirect() in Asp.Net WebForm
 return RedirectToAction("MyIndex");
}

public ActionResult MyIndex()
{
 ViewBag.Msg = ViewBag.Message; // Assigned value : Null
 return View("MyIndex");
}

2) Using RouteConfig.cs in App_Start folder
For this try this link[^]

I hope this helps you a bit

Regards,
RK
 
Share this answer
 
I solved this by using this mapping of route.



C#
routes.MapRoute(
          name: "CMS",
          url: "{controller}/{category}/{id}",
          defaults: new
          {
              category = UrlParameter.Optional,
              page = 36,
              action = "Content"
          },
          constraints: new
          {
              controller = "Home"
          }
     );



I put the category optional and controller as constraint, so it worked with me.
 
Share this answer
 

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