Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
when i give the route as follows.
it redirect to the page correctly.
but the url not displaying correctly.
instead of "AppDevelopment/Info",it is displaying as "Home/Info/Apps"


routes.MapRoute("Apps", "AppDevelopment/Info", new { controller = "Home", action = "Info", id = "Apps" });


please help me to resolve this
Posted

What would be helpful is to see your entire Register Routes method. With setting up routes in MVC3 the order you have them is the most important. You should not have a new route you created below the Default route. Any new routes you create must come before the Default route...otherwise it wont work. So that is why i say seeing your routes method would be helpful.

So below is the routes method that works in my sample mvc 3 project

I would also am wondering why you are forcing an Id in your route. If your forcing an Id from the route why can't you just hard code it in your controller. So going off your example if i wanted to see the app development info for my MVC App...well i cant slap it on the end of the URL like AppDevelopment/Info/MvcApp because you've hard coded the route to always use the Id of "Apps"...so whether i type AppDevelopment/Info or AppDevelopment/Info/Your Grandma...the Id for that route will always be Apps. This is a bad practice in my opinion.

The other thing to note is that you are hard coding the into your route URL template the Action of the route and then also specifying the Action within the defaults...because you've hardcoded it into the URL template...you would not need to specify it in the defaults.

C#
// Routes Method in RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
	routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
	
//My new route is above the Default Route
	routes.MapRoute(
		name: "Apps",
		url: "AppDevelopment/Info",
		defaults: new { controller = "Home", action = "Info", id = "Apps" });

	routes.MapRoute(
		name: "Default",
		url: "{controller}/{action}/{id}",
		defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
	);
}


C#
// My home controller in my sample...notice no Id is specified for the Info route...because you've hardcoded into your route in the config.
public class HomeController : Controller
{
	public ActionResult Index()
	{
		return View();
	}

	public ActionResult Info()
	{
		ViewBag.Message = "Your application description page.";

		return View();
	}

	public ActionResult Contact()
	{
		ViewBag.Message = "Your contact page.";

		return View();
	}
}


What i would consider as the better way of going about and implementing this route to do what you want but be flexible enough to scale based on your need is below.

C#
//Better implementation of the route to allow for flexibility
public class RouteConfig
{
	public static void RegisterRoutes(RouteCollection routes)
	{
		routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

		// Sample second route to demo how/where to add an additional route
		routes.MapRoute(
			name: "AppsRoute2",
			url: "PersonalDevelopment/{action}/{id}",
			defaults: new { controller = "Home" });

		// notice that the Id for this route is {appName}...because of that you will need to make sure that they paramtere within the method (action) of the controller match the name 
		//      used in the route. If they don't match...your route will not work. Say take note on the Home/Info controller the parameter name for Info
		//
		//  Also by sepcifying the action to be {action}  in this route...you can allow the home controller to 
		//      respond to any request that starts with AppDevelopment/MyCustomHomeController Method Here instead of only being Info.
		//
		//  To get it where you've hard coded the Info into the route...reverse the comments on the two Url parameters (make the comment out the uncommented one and uncomment the commented out one)
		routes.MapRoute(
			name: "Apps",
			url: "AppDevelopment/{action}/{appName}",
			//url: "AppDevelopment/Info/{appName}",
			//defaults: new { controller = "Home", action = "Info" });
			defaults: new { controller = "Home"});
		
		routes.MapRoute(
			name: "Default",
			url: "{controller}/{action}/{id}",
			defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
		);

		

	}
}


Now for the controller...like comment in code above...take note of the param name in the Info action (method).

C#
public class HomeController : Controller
{
	public ActionResult Index()
	{
		return View();
	}

	public ActionResult Info(string appName)
	{
		ViewBag.Message = "Your application description page.";

		return View();
	}

	public ActionResult Contact()
	{
		ViewBag.Message = "Your contact page.";

		return View();
	}
}


Sample View to see the Id within each stage of the above code. Can use this code to see that hard coding the route will override any Id you pass in on the URL in first bit of code and then to also see that what you pass in from the URL using the second route example will reflect whatever is on the route.

HTML
To view with hardcoded Id route value
<br />
@Url.RequestContext.RouteData.Values["id"]
<br /><br /><br /><br /><br /><br />
<br /><br />
Route Id result using the Id of the route that was changed to a custom Id and passed in over the viewbag
<br /><br />
@ViewBag.Message


So now with the above code that i've given you...the following routes work http://localhost:54425/AppDevelopment/Info/MyMvcApp[^]

http://localhost:54425/AppDevelopment/Info/My%20Second%20App[^]

And the URL that you were looking for based on your question

http://localhost:54425/AppDevelopment/Info/Apps[^]

Let me know if you have any questions on the code above i'd be happy to help you understand it
 
Share this answer
 
v4
I have created different controller and action method.

then i used redirecttoroute(name) inside action method.

like below:

AppDevelopment Controller:

public ActionResult Info(string Id)
{
return RedirecttoRoute(Id);
}

Thanks friends,

Regards
Lalitha
 
Share this answer
 
Where Default Routeconfig.cs
routes.MapRoute(
               name: "Default",
               url: "{controller}/{action}/{id}",
               defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
           );

{id}:Option parameter
No need to fix from here
See this MapRoute setting will effect on page load time.
By default id is optionparamter and where you are fixing with "Apps" value that's why id is coming with URL
 
Share this answer
 
v3

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