I changed the default routing in ASP.NET MVC from
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
to
routes.MapRoute(
"Default",
"{controller}/{action}/{id}/{lineNo}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional, lineNo = UrlParameter.Optional });
but now all #Html.ActionLink() calls are rendered to href="". If I change the route back to default all links are working again.
I used the same route with RC1 and it worked perfectly.
I didn't find anything in the release docs so I think I'm doing it wrong.
Regards,
Steffen
In a route an optional parameter can appear only at the end. This means that in your route definition the id parameter cannot be optional. You need to explicitly set it to a value.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}/{lineNo}",
new {
controller = "Home",
action = "Index",
lineNo = UrlParameter.Optional
}
);
And when you generate a link you must always provide a value for the id parameter if you want this route to match:
#Html.ActionLink("some link", "index", new { id = "123" })
As an alternative you might give a default value to the id parameter:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}/{lineNo}",
new {
controller = "Home",
action = "Index",
id = "123",
lineNo = UrlParameter.Optional
}
);
Now you no longer need to specify it in your links.
Related
In my limited (2 weeks) experience in asp.net MVC3, for most action methods, I have never needed to add a route registration. But I have noticed that if the action method has an input parameter, then I can't access the method with a url of the form www.mysite.com/myController/myAction/myParameter1/myParameter2/myParameter3 (without the ? mark ) unless I map the route. Is that how its supposed to be?
By default, you already have registered route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
it accepts one parameter, named id, so your action:
public ActionResult MyAction(string id)
will "catch" the request:
www.mysite.com/MyController/MyAction/parameter_value
and id will get value "parameter_value".
If you need more than one parameter (or parameter has to be names something else than "id"), then you have to register new route.
In case when you have 2 parameters, you will register route like this:
routes.MapRoute(
"Default",
"{controller}/{action}/{parameter1}/{parameter2}",
new { controller = "Home", action = "Index", parameter1 = UrlParameter.Optional, parameter2=UrlParameter.Optional }
);
and your action might be:
public ActionResult MyAction(string parameter1, int? parameter2)
Yeah, you need to register the route customizing the route in global.asax according to your requirement.You have to register the route in following way:
routes.MapRoute(
"routeName", // Route name
"{controller}/{action}/{myParameter}", // URL with parameters
new { controller = "Home", action = "Index", myParameter= "" } // Parameter defaults
);
So with above route, it ensures that whenever your url goes in above format, the parameter right after "action/" will be taken as parameter.....
For more than one parameter in your url, you can register like this:
routes.MapRoute(
"routeName", // Route name
"{controller}/{action}/{myParameter1}/{myParameter2}/{myParameter3}", // URL with parameters
new { controller = "Home", action = "Index", myParameter1= "", myParameter2= "", myParameter3= "" } // Parameter defaults
);
I have an application that I am using a global route to query for the current path and return page specific data. I have the routes setup like this...
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Pages",
"Pages",
new { controller = "Pages", action = "Index" });
routes.MapRoute(
"Navigation",
"Navigation",
new {controller = "Navigation", action = "Index"});
routes.MapRoute(
"Default", // Route name
"{*url}", // URL with parameters {controller}/{action}/{id}
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The problem I am facing is when I go to /Pages to try and add new pages, the PageController fires like it is supposed to, but when debugging, after going to /Pages the app then makes a request for the HomeController. Am I missing something in my routing setup?
The Default route is firing because of the {*url}. So any page that's not /Pages, will go to the default route.
I need more info, but if you're trying to do /Pages/whatever, then you need to add an optional parameter on your Pages route:
routes.MapRoute(
"Pages",
"Pages/{page}",
new { controller = "Pages", action = "Index", page = UrlParameter.Optional });
Your default route is incorrect. It should look like the default route as defined when you open a new MVC 3 project, like so:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The problem is that the default route you defined will not parse any requests which reach it.
I am working on a project, where I have used the default routing
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/{name}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
I have a users controller and methods like Index, create and details
I dont want to create links like http://mysite/users/details/111/somename
instead i want to create http://mysite/users/111/somename
like stackoverflow kind of url.
I know I can achieve by registering a route like this (before default route)
routes.MapRoute(
"UsersDetails", // Route name
"Users/{id}/{name}", // URL with parameters
new { controller = "Users", action = "Details", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
but then all my other urls start creating problem. for example if i have url
http://somesite/users/registersuccess
Is there in workaround for this issue, or I should register all the urls.
Regards
Parminder
You'd need a constraint on your id parameter for your routes:
routes.MapRoute(
"UsersDetails",
"Users/{id}/{name}",
new { controller = "Users", action = "Details", id = UrlParameter.Optional, name = UrlParameter.Optional },
new { id="\d+" }
);
Then your other routes wouldn't be affected.
HI I have the following routing table
routes.MapRoute(null,
"Save", // Route name
new { controller = "Package", action = "Save"} // Parameter defaults
);
routes.MapRoute(
"Package", // Route name
"{controller}/{action}/{name}/{p}", // URL with parameters
new { controller = "Package", action = "Index", name = UrlParameter.Optional, p = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
When i am typing the path /Package/Save it is showing me page not found.
Can anybody please tell me what i am doing wrong?
THanks
The first route is configured incorrectly - name of route, then the pattern and then the defaults.
Also make sure you have a Package controller and a Save action method on it.
Greetings,
I have a problem with link in mvc application. When I run it via Visual Studio it's ok. The link then is as follows:
http://localhost:2566/ActivateClient/Activate/6543e2d6-707d-44ae-94eb-a75d27ea0d07
when I run it via IIS7 the link is as follows:
http://localhost/ActivationService/ActivateClient/Activate/6543e2d6-707d-44ae-94eb-a75d27ea0d07
The default route is as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
I assume I have to change this MapRoute, am I right? How to change it? ActivationService is my virtualDirectory in IIS. Can someone help me with this please?
I also tried to maproute as follows:
routes.MapRoute(
"Default", // Route name
"ActivationService/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
but also without success
Did you add the new one or replace the existing one?
If you added, you need to position it before the existing one.
routes.MapRoute(
"Default", // Route name
"ActivationService/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The rules take precedence..