asp.net mvc routing pattern is
{"some_parameter/{controller}/{action}/{id}"}
Is this a valid format if some_parameter can be null or string empty
I believe that what you wanted is {some_parameter}/{controller}/{action}/{id} (notice curly brackets around "some_parameter") and in that case it shouldn't be null or empty, I think. How do you think your end URL might look like to match the route in case when some_parameter is empty? "mysite.com//mycontroller/myaction/myid"?
Routing engine just matches patterns. If you want to handle both {some_parameter}/{controller}/{action}/{id} and {controller}/{action}/{id}, just define both routes.
Edit
I've just reordered the route registration so that it would work:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "home",
action = "index",
id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{some_parameter}/{controller}/{action}/{id}", // URL with parameters
new {
some_parameter = UrlParameter.Optional,
controller = "home",
action = "index",
id = UrlParameter.Optional
}
);
They should be registered in that order. Additionally the second route requires an id and some_parameter parameter otherwise it will never be hit because of the route before it. Even though the some_parameter and id parameters are set to optional, that would never happen because the route before would catch it if it was empty.
Related
The default route map specified in MVC is:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
This will allow a URL like http://mysite.com/controller/action/id
Having read other posts on stackoverflow, I had the impression (incorrectly) that to add SEO information to my MVC urls I could simply change the route map to:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/{seo}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, seo = UrlParameter.Optional } // Parameter defaults
);
Which would allow a URL like http://mysite.com/controller/action/id/information-for-search-engines
It DOES in fact route correctly, but for some reason it now calls the action THREE TIMES?? Is there something basic I have done wrong here?
What am I doing wrong?
My default /User route
routes.MapRoute(
"User", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "User", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
I want to separate code so I create another controller "UserProducts"
my route
routes.MapRoute(
"UserProducts", // Route name
"user/products/{action}/{id}", // URL with parameters
new { controller = "UserProducts", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I have ActionResult Index in my UserProducts controller, but however my
localhost/user/products
doesn't work:
Error 404 - The resource cannot be found.
You probably have them in the wrong order. The order in which you register these routes is significant, and the first mapping will override the ones after it. Put the UserProducts line above the one for User.
When I use Html.ActionLink() the URL created is not in the desired format:
Html.ActionLink(Model.ProductCode, "Update", new { id = Model.ProductId })
Makes this URL
/Update?id=1
When I want to have this URL:
/Update/1
What routing options create the 2nd URL? This is our preferred URL style.
Both URLs work and the correct page is displayed - however we want to only use /id
In Global.asax the MVC default route handles both URLs
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }); // Parameter defaults
I can replicate the issue by having a route about my default route that still matches the general pattern. Example:
routes.MapRoute(
"Default2", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
When places above my default route, I get the ?id=1 in my URL. Can you confirm that this ActionLink is not matching any routes above the route that you are expecting it to match?
EDIT: The below does not impact the URL
However, it could still be advantageous to use the UrlParameter.Optional in other scenarios. Leaving for prosperity unless mob rule says otherwise.
new UrlParameter.Optional value. If you set the default value for a
URL parameter to this special value, MVC makes sure to remove that key
from the route value dictionary so that it doesn’t exist.
I think you need to adjust your route slightly. Change id = "" to id = UrlParameter.Optional
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
This is what we use for the default route and the behavior that you are looking for is how our applications behave.
See This question/answer.
Which version of MVC are you using? If you're in MVC3, you'll need to add a fourth parameter to your call to Html.ActionLink(), passing in a null.
I've just stumbled upon this and decided to answer. It turned out that both Url.Action() and Html.ActionLink() use the first route in the route collection to format the resulted URL. So, the first mapped route in the RegisterRoutes() shoild be:
routes.MapRoute(
name: "Default",
url: "{controller}/{id}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
instead of "{controller}/{action}/{id}". The route name (i.e. "Default") does not matter, only the order does matter
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I'm interested to know how people handle the following situation.
Assume we have a DataField and each DataField can have unlimited number of DataValues
We have 2 controllers to handle the manipulation of these objects
DataFieldController
DataValueContoller
Now if we ever need to add a new DataValue we need to know the ID of the CustomDataField. The following URL would be used,
/CustomDataValue/Add/1
1 = DataField ID
However, because the ASp.Net MVC engine binds the parameter name to the model (IE in the case below. My DatValeu Object would have its ID replaced, when I am actually trying to pass through the FieldID)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Site", action = "Home", id = UrlParameter.Optional } // Parameter defaults
);
How can we handle this? Doing the following obviously will not work.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Site", action = "Home", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{fieldid}", // URL with parameters
new { controller = "Site", action = "Home", fieldid = UrlParameter.Optional } // Parameter defaults
);
I assume this is a common problem, I just cant find the obvious solution at the moment. It would be ok if the Signature was differant but both are /String/String/Int
==========================
How can these routes work then?
/DataValue/Add/{DataFieldID}
/DataValue/Edit/{ID}
/DataValue/List/{DataFieldID}
Must I add 3 routes?
Use constraints in routes like this:
routes.MapRoute(
"Default", // Route name
"CustomDataValue/{action}/{fieldid}", // URL with parameters
new { controller = "Site", action = "Home", fieldid = UrlParameter.Optional } // Parameter defaults
);
It makes sure only URLs starting with "CustomDataValue" calls this route. It's declared as a constant, different from the default route. Make sure these specified routes are declared before the default route. Since there are no restrictions, all URLs are matched to it.
Update
I guess you have to call DataValueController methods with URLs like http://domain.com/CustomDataValue/Add/23. If that's the case use the following route:
routes.MapRoute(
"CustomData", // Route name
"CustomDataValue/{action}/{fieldid}", // URL with parameters
new { controller = "DataValue", action = "List", fieldid = UrlParameter.Optional } // Parameter defaults
);
This will work if you have action methods in DataValueController named List/Add/Edit.
What is the problem below?
routes.MapRoute(
"Default2", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "test" } // Parameter defaults
);
routes.MapRoute(
"Default1", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Report", name = "" } // Parameter defaults
);
When I navigate to /home/index "id" parameter takes the default value of "test" but when I navigate to home/report the name parameter is null.
In short, if the route definition is the first in the route table, then the parameter takes its default value. But the others below don't.
These two routes {controller}/{action}/{id} and {controller}/{action}/{name} are ambiguous. It cannot distinguish between /home/index/id and /home/report/abc, it is always the first route in the route definition which will be caught because in the second case it thinks that id = "abc".
Use Phil Haack Routes debugger.. to get more clear view how your routes are reacting on diferent paths.
download