How many routing parameters can you have in .NET MVC?
Like localhost:port/controller/action/parameter1/parameter2/parameter3/ and so on
I'm currently using this for my routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{extra}/{extra2}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extra = UrlParameter.Optional, extra2 = UrlParameter.Optional }
);
And this works great.
But when I add yet another routing like an "extra3" in this example the whole page just breaks, ordinary form posts and so on stops working.
Is there a limit to how many you can have and should I rather just make normal querystrings?
Edited
To explain it further.
I want to do this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{extra}/{extra2}/{extra3}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extra = UrlParameter.Optional, extra2 = UrlParameter.Optional, extra3 = UrlParameter.Optional }
);
But it just simply does not work when I add even further amount of routes/parameters.
Is there a limit to how many you can have?
HTML Helpers e.g. (Html.ActionLink, Html.Action, Html.BeginForm etc) gives wrong results when there are more optional parameters in routs. Normally we keep one optional parameter at the end of the route as optional.
If you keep the routing config as follows it will work.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{extra}/{extra2}/{extra3}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extra = UrlParameter.Optional, extra2 = UrlParameter.Optional, extra3 = UrlParameter.Optional }
);
routes.MapRoute(
name: "OneOptional",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Read the section "The Root Cause" of following post understand the issue with multiple optional parameters.
http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx/
Thanks!
Related
I'm setting up a new server with MVC 5.2.4.0 in ASP.NET. My problem is with the route configuration.
In the configuration file App_Start/RouteConfig.cs I have put the following code:
routes.MapRoute(
name: "DefaultEvent",
url: "{idseries}/{controller}/{action}/{id}",
defaults: new { idseries= "startgate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultEdition",
url: "{idseries}/{idsequel}/{controller}/{action}/{id}",
defaults: new { idseries= "stargate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I want to use this urls which format are:
url:port
url:port/stargate
url:port/stargate/episode/actors
url:port/stargate/episode/actors/ONeill
url:port/stargate/SG1
url:port/stargate/SG1/episode/actors
url:port/stargate/SG1/episode/actors/ONeill
I had understood that ASP.NET tried to find the first MapRoute that does not fail, but it is not like that.
I have used Visual Studio Debugger but this only enters the file RouteConfig.cs at the first execution.
Also, I have created MapRoutes to cover all the combinations but it only enters the first entry. And if it does not enter it fails, showing the 404 error page.
Examples:
routes.MapRoute(
name: "DefaultEvent",
url: "{idseries}/{controller}",
defaults: new { idseries= "startgate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultEdition",
url: "{idseries}/{idsequel}",
defaults: new { idseries= "stargate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
With this configuration it fails if I do not put a valid driver.
routes.MapRoute(
name: "DefaultEdition",
url: "{idseries}/{idsequel}",
defaults: new { idseries= "stargate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultEvent",
url: "{idseries}/{controller}",
defaults: new { idseries= "startgate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But if I put it upside down, it counts as the id of the sequel (which I see logic).
I have an custom mvc route it's not working. if i define the route before home route then it's working otherwise not.
this code is not working.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Citysearch",
"{state}",
new { controller = "Dashboard", action = "GetDynamicContent" }
);
when i define the citysearch first then it's working something like this
routes.MapRoute(
"Citysearch",
"{state}",
new { controller = "Dashboard", action = "GetDynamicContent" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
and another problem is this is the url of city search http://localhost:51381/dynamic-content. dynamic-content this is my state paramter. It's calling of my Dashboard/GetDynamicContent. but problem is when application run after login url is http://localhost:51381/Home it is calling always Dashboard/GetDynamicContent how to get rid of this problem please help me.
Routes are read from top to bottom. Therefore, the first route match will be used when routing.
Try this
routes.MapRoute(
"Home",
"Home/{action}",
new { controller = "Home", action = "index" }
);
routes.MapRoute(
"Citysearch",
"DynamicContent/{state}",
new { controller = "Dashboard", action = "GetDynamicContent" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You have to use some fix part in your url as shown below DynamicContent/ to solve issue you are facing. And your url should be like http://localhost:51381/DynamicContent/dynamic-content.
routes.MapRoute(
"Citysearch",
"DynamicContent/{state}",
new { controller = "Dashboard", action = "GetDynamicContent" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I would like to add my experience in this issue "custom-map-route is not working".
Myobservation:custom url routing not working when page is redirected using href="redirected page" . when i am using #Url.Action() for redirection then custom routing started working.
here is my initial code(peice of code)
return '<a href="/Home/AuthorityForm?formid=' + '#ViewBag.formid' + "&SubmittedFormId=" + full.ConsentId + """ + 'target="_blank">ViewForm';
i changed to
var link = '#Url.Action("AuthorityForm", "Home", new { formid = ViewBag.formid, SubmittedFormId = "-1" })';
link = link.replace("-1", full.ConsentId);
return 'ViewForm'
this may help someone..
I have rather a simple routing map.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{seoName}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, seoName = UrlParameter.Optional }
);
Now, If use Url.Action("Index", "Home"), it does not properly remove defaults values of the route. And it gives me /Home/Index.
Now, if I remove either {id} or {seoName}, and its corresponding default value, then the URL is properly generated like / (root).
What I am missing here? It does not seem to be an ambient value, since I am visiting the main page with no ids, nor seoNames.
Any ideas?
You would need multiple mappings to achieve what you want as you are only allowed to make the last route placeholder optional.
routes.MapRoute(
name: "SeoFriendly",
url: "{controller}/{action}/{id}/{seoName}",
defaults: new { controller = "Home", action = "Index", seoName = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I need to call customer profile page like "(www.mysite.com/John) or (www.mysite.com/customer name)"
so i had add route to be like that
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
but it always go to the first route as if i need to open any controller it does n`t work
any advice?
Thanks.
the route wont work like that so you have two options
1) When going to any action your URL should be www.mysite.com/controller/action or else it will give HTTP 404 error
2) You can make route unique by adding prefix www.mysite.com/Customer/John like this
routes.MapRoute(
name: "Profile",
url: "Customer/{id}",
defaults: new { controller = "Home", action = "Index"}
);
and your action should be
public ActionResult Index(string id)
{
string SurveyName = "";
if (id != null)
SurveyName = id;
if (!string.IsNullOrEmpty(SurveyName))
{
ViewBag.Survey = SurveyName;
}
return View();
}
You need to change your controller and action according to your requirement, you are still providing default values to controller and action.
You can also use Html.ActionLinkat to refer to your desired view
ex. Html.ActionLink("action_name","Controller_name")
Your route has a single optional parameter and is working like a catch all route.
[EDIT]
You can't use the constraint as I said on my previous answer if you don't have an pattern to restrict the route. Based on this you can remove the id = UrlParameter.Optional from defaults (by this change your route will be reached only when the url contains the id parameter).
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "Employee", action = "Index"}
);
Another option is to create a custom route constraint to check if your profile id is valid but take care since it can slow down your application since it'll be executed on every request (maybe caching your profile list could help on this).
[OLD ANSWER]
An easy way to restrict a route scope is using constraint.
The route below is reached just if the id parameter is a value with 4 digits (you can use your own regex to fit your requirements).
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "Employee", action = "Index"},
constraints: new { id = #"\d{4}" }
);
Make these changes. It worked for me.
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "NewCon", action = "Demo", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I'm trying to setup a route that's routes to a div on a page in my RouteConfig.cs file but doesn't work (HTTP Error 403.14 - Forbidden).
routes.MapRoute(
name: "Default",
url: "{controller}/{action}#{locId}/{id}",
defaults: new { controller = "Home", action = "Index", locId =
"theDiv", id = UrlParameter.Optional }
);
Can somebody help me out please?
Thanks in advance.
This should do it,
routes.MapRoute(
name: "WithId",
url: "{controller}/{action}/{id}#{HTMLElementId}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, HTMLElementId = UrlParameter.Optional }
);
this sets it as an optional parameter.
thanks
Try to build your route differently using this format :
url:"{controller}/{action}/{id}#{locid}",