I am working with internationalization and long routes.
My URLs look like domain/en-us/users/edit/240.
Here is my RouteConfig :
routes.MapRoute(
name: "DefaultWithCulture",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { culture = "([a-zA-Z]{2}-[a-zA-Z]{2})|[a-zA-Z]{2}" }
);
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
And here is some Actions in my Controller Users :
public ActionResult Edit(int id);
public ActionResult EditPassword(int id);
public ActionResult EditRights(int id);
I want my Actions EditPassword and EditRights to be accessible via domain/en-us/users/edit/password/240 and domain/en-us/users/edit/rights/240.
I did find a solution, using Route Attributes but with the culture (en-us, es-mx, fr-ca) in my url, it broke it all.
The DefaultWithCulture route you have configured will match 3 or 4 route segments. Your URL (en-us/users/edit/password/240) has 5 route segments, so it will not match.
It will match if you pass it the action method name as you have it configured: en-us/users/editpassword/240.
If you want your URLs to look like your example with 5 segments, you will need to do some additional work. First of all, you need to account for the fact that your action names and URLs don't match. One way to do that is to use the ActionName attribute.
[ActionName("password")]
public ActionResult EditPassword(int id);
[ActionName("rights")]
public ActionResult EditRights(int id);
Then you need some new routes and constraints to match the 4 or 5 segment URLs. The main issue to deal with is that some of your segments overlap. So, you need a constraint so you can tell when the 4th segment is an id or if it is an action method.
Basically, to do this break the 1 route with an optional id parameter into 2 routes with required parameters (since when you add a constraint to id it makes it required).
Then add another route for both the localized and non-localized version that handles the extra edit segment in your URL.
// Route to handle culture with 4 segments, ending in numeric id
routes.MapRoute(
name: "DefaultWithCulture",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index" },
constraints: new { culture = "([a-zA-Z]{2}-[a-zA-Z]{2})|[a-zA-Z]{2}", id = #"\d+" }
);
// Route to handle culture with 3 segments, to make id optional
routes.MapRoute(
name: "DefaultWithCulture3Segments",
url: "{culture}/{controller}/{action}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index" },
constraints: new { culture = "([a-zA-Z]{2}-[a-zA-Z]{2})|[a-zA-Z]{2}" }
);
// Route to handle culture with 4 or 5 segments
routes.MapRoute(
name: "DefaultWithCulture5Segments",
url: "{culture}/{controller}/edit/{action}/{id}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { culture = "([a-zA-Z]{2}-[a-zA-Z]{2})|[a-zA-Z]{2}" }
);
// Route to handle default with 3 segments, ending in numeric id
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { id = #"\d+" }
);
// Route to handle default with 2 segments, to make id optional
routes.MapRoute(name: "Default2Segments",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
// Route to handle default with 3 or 4 segments
routes.MapRoute(name: "Default4Segments",
url: "{controller}/edit/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If you want the extra segment you added to read something other than edit, you will need additional routes because MVC doesn't understand this URL scheme natively.
Related
I am using MVC 4 and need to remove /Home/ folder from address bar...
Eg:
http://localhost:61700/Home/AboutUs
Need to be changed as...
http://localhost:61700/AboutUs
I did that by changing the default controller in "RouteConfig.cs"
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
//url: "{controller}/{action}/{id}",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
The above code is working as expected. I do have another folders as
brand, admin etc... here I want to show the url as
http://localhost:61700/brand/productInfo ... But I am getting server
error here as Server Error in '/' Application.
Can somebody suggest me, where am I doing wrong?
Screenshots here for more info:
This is your current RouteConfig.cs configuration:
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You're telling Asp.net, when a request arrives, assume the first parameter as the action and the second parameter as the id. Right now you're not telling Asp.net to parse any parameter as the controller. Because of this it uses the default value (given as the third parameter of the MapRoute method) which is in this case Home.
In that case when parsing the request http://localhost:61700/AboutUs the values end up being:
controller: Home (it uses the default controller)
action: AboutUs (from the first parameter)
id: null (this doesn't matter right now)
When parsing the request http://localhost:61700/brand/productInfo the values end up being:
controller: Home (it uses the default controller because you haven't specified where to get the controller name from)
action: Brand (from the first parameter)
id: "productInfo"
The error you're getting is because there isn't a Brand action method in HomeController.cs with a parameter of type string named id.
Asp.net processes incoming requests by trying to match with the routes configured and it uses the first route that matches.
There are several ways to achieve what you want, which include but are not limited to:
Manually mapping every action in your HomeController.cs (choosing this method will depend on the amount of actions in your HomeController). This would look like:
routes.MapRoute(
name: "AboutUs",
url: "AboutUs",
defaults: new { controller = "Home", action = "AboutUs" }
);
routes.MapRoute(
name: "ContactUs",
url: "ContactUs",
defaults: new {controller = "Home", action = "ContactUs" }
);
// etc...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Note how the default route is the last one, this is important because it is less specific than the others and if put before would match the request and want to look for an AboutUsController.
You could use route constraints. This would look like:
route.MapRoute(
name: "HomeControllerRoutes",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { action = "AboutUs|ContactUs|etc..." } //Here you would put all your action methods from home controller that you want to accces as /{action}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If you want to read more about route constraints, I found this article that explains that the constrains parameter can receive a regular expression (I suggest you modify the regular expression above to make it case insensitive) or an IRouteConstraint.
Update:
I just read your comment about having 160+ actions in your HomeController that would make your regular expression in my second suggestion quite long. In that case the other options you have could be:
Using a regular expression that rejects all other controller names, but that would violate the open/closed principle (OCP) and every time you add another controller you would have to add it to the regular expression.
Create the regular expression from the metadata of you HomeController class. This would look like
string.Join("|", typeof(HomeController).GetMethods().Select(info => info.Name))
Or you could take a look at IRouteConstraint to see if you could figure out a more elegant solution.
I have no experience with IRouteConstraint
Add this in your route.config / glibal.asax and don't change your default routes. Add following above it.
routes.MapRoute(
name: "About",
url: "AboutUs",
defaults: new { controller = "Home", action = "AboutUs" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have 160+ views in the home controller
You don't mention how many views you have in the other controllers, nor how complicated they need to be.
Rather than keep the default controller/action and add routes for every view in home, you can add a route for each controller and then have your default route without a controller path.
While this means you do need a route for every controller, it's better than one for every view.
routes.MapRoute(
"AdminRoute",
"Admin/{action}/{id}",
new { controller = "Admin", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"BrandRoute",
"Brand/{action}/{id}",
new { controller = "Brand", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"HomeRoute",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"DefaultRoute",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
(afaicr you don't need the default route as all your views would be covered by the other 3 routes)
Note the path for 'HomeRoute' doesn't have a controller part.
As long as they are in this order any url with /Admin/ or /Brand/ will be picked up first.
Is there a way to define route like this
routes.MapRoute(
name: "Language",
url: "{controller}/{action}.{culture}",
defaults: new { controller = "Home", action = "Index", culture = UrlParameter.Optional }
);
And be able to handle url like http://www.domain.com/Test/Create.us-US?
Yes, you can do it, but add route for url like http://www.domain.com/Test/Create without dot at the end
routes.MapRoute(
name: "Language",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", culture = "us-US" }
);
No you can create route without with dot but you use the special caracter to the root. When we declare the method at the controler at that time you can specify the Action name define in the route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "DashboardV1_bm", id = UrlParameter.Optional }
);
[HttpPost, ActionName("DashboardV1_bm")]
[ValidateAntiForgeryToken]
public ActionResult DashboardV1(int id)
{
Shift shift = db.Shifts.Find(id);
db.Shifts.Remove(shift);
db.SaveChanges();
this.AddToastMessage("Delete", "Record is successfully deleted !", ToastType.Success);
return RedirectToAction("Index");
}
For more information, please visit my blog:
http://programmingcode.in/create-routemap-to-access-new-created-view-page-in-mvc-dot-net/
I want users to be able to access the "/Linecard" page of my ASP.Net MVC site using "/Linecard" or "/Manufacturers" as the URL... so same controller, 2 different possible URLs.
I tried adding the following:
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
Adding this after the "Default" route doesn't work at all and I get a 404 error when I go to "/Manufacturers". Putting it BEFORE "Default" works, but then only "/Manufacturers" shows up in the URL when I click menu links since it is the first match. I would like "/Linecard" to always show as the URL.
Any pointers? Is there a certain constraint I can use to accomplish this? Thanks!
I had the same problem when we moved to extension-less URLs. We needed to continue to support one route with extensions. I got around it by having my default route apply to everything except the old URL, then after that mapping one specifically for the exception
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
// if controller specified does not match 'manufacturers' (case insensitive)
new { controller = "^((?i)(?!manufacturers).)*$" },
new string[] { "Namespace.Of.Controllers" }
);
routes.MapRoute(
"Manufacturers", // Route name
"Manufacturers/{action}/{id}", // URL with parameters
new { controller = "Linecard", action = "Index", id = UrlParameter.Optional },
new string[] { "Namespace.Of.Controllers" }
);
You could also set an order when mapping your routes with the defaults at the end like so
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I'm adding a new route into my mvc web application and it's not working as desired. I was hoping someone could help me figure out where in the list it should go and maybe which defaults and/or constraints should be defined for it (in RouteConfig.cs).
The desired route would look like so:
/controller/id/slug/action e.g. mydomain.com/products/10/product-name/reviews
I've tried to define this route like so and have tried it as the 1st, 2nd and 3rd routes listed:
routes.MapRoute(
name: "AlternateRoute",
url: "{controller}/{id}/{slug}/{action}",
defaults: null,
constraints: new { id = #"\d+", slug = #"[\w\-\d+]*" }
);
What's happening is after I add the above route, and browse to a page like /products/10/product-name - url's that were previously something like /products/create look like /products/10/product-name/create (but only on that page).
The only other routes I have are these 3 (defined in my routeConfig file):
/controller/id/slug
routes.MapRoute(
name: "DefaultSlugRoute",
url: "{controller}/{id}/{slug}",
defaults: new { action = "Details", slug = "" },
constraints: new { id = #"\d+", slug = #"[\w\-\d+]*" }
);
/controller/action/year/month
routes.MapRoute(
name: "MonthlyArchiveRoute",
url: "{controller}/{action}/{year}/{month}",
defaults: new { controller = "Blog", action = "Archives", year = UrlParameter.Optional, month = UrlParameter.Optional },
constraints: new { year = #"\d{4}", month = #"\d{2}" }
);
/controller/action/id (the standard one included w/ a new mvc project)
routes.MapRoute(
name: "DefaultRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have this URL:
/controller/action/value
and this action:
public ActionResult Get(string configName,string addParams)
{
}
How do I set up my routing table to get the routing engine bind the value to the configName parameter for any action in the Config controller?
Well, first off, that is incomplete. You don't have a method name.
Secondly, this will already work with URLs of the format:
/controller/action?configName=foo&addparams=bar
Here's how to do it with pretty routes:
routes.MapRoute(
"YourMapping",
"{controller}/{action}/{configName}/{addParams}");
or
routes.MapRoute(
"YourMapping",
"{controller}/{configName}/{addParams}",
new {
controller = "YourController",
action = "YourAction"
},
new {
controller = "YourController" // Constraint
});
if you want to exclude the action from the URL.
You could add a new route above the default
routes.MapRoute(
"Config",
"config/{action}/{configName}/{addParams}",
new { controller = "Config", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Which will allow you to use the route /config/actionName/configName/addParamsValue. Your other routes should be unaffected by this.
routes.MapRoute(
"ValueMapping",
"config/{action}/{configName}/{addParams}",
new { controller = "Config", action = "Index", configName= UrlParameter.Optional, addParams = UrlParameter.Optional } // Parameter defaults);
Setting default Controller to Home, with a Default Action of Index
So the Url:
/config/get/configNameValue/AddParamValue
would match this Method:
public ActionResult Get(string configName,string addParams)
{
//Do Stuff
}