How to choose route in MVC3 - asp.net-mvc

I have global.ascx with three routes
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional },
new { id = #"\d+" } //one or more digits only, no alphabetical characters
);
routes.MapRoute(
"TestCatalogRoute",
"{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } // Parameter defaults
//new { controller = "Product", action = "Index2", id = UrlParameter.Optional } // Parameter defaults
);
}
When I enter url:
http://mydomain.com/
It uses "TestCatalogRoute" route, but I want "Default" route T.T
How to:
with url: http://mydomain.com it uses "Default" route
with url: http://mydomain.com/1 it uses "TestRoute" route (It's already done!)
with url: http://mydomain.com/abc it uses "TestCatalogRoute" route

Remove id = UrlParameter.Optional for TestCatalogRoute then

Change the order of your routes. The routehandler will validate each route, first one that matches will get picked. So if you put the second one last, you should be fine?

I can recomend using Routing Debugger to debug your route easy to use.
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

Related

Multiple optional parameters in MVC is not working

My requirement is to provide optional parameters to urls. urls should be like the.
http://test.com/118939
http://test.com/118939/test/2000/
I have written following routes
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"FAQDefault",
"FAQ",
new { controller = "FAQ", action = "Default" });
routes.MapRoute(null, "{id}", new { controller = "Home", action = "Default", id = UrlParameter.Optional });
routes.MapRoute("rent", "{id}/{rent}/{unit}", new { controller = "Home", action = "Default", id = UrlParameter.Optional, rent = UrlParameter.Optional, unit = UrlParameter.Optional });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Default", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "CDCPortal" });
}
and controller written like:
public ActionResult Default(string id, string rent=null,string unit=null){}
Its working fine for 1 url but not working for second url.
There is no need to do as, you have done in the first case:
The second route can handle any number of parameters.
You need to define a route for each combinations like below
routes.MapRoute("Default-AllOptional",
"Default/{id}/{rent}/{unit}",
new
{
controller = "Home",
action = "Default"
// nothing optional
}
);
routes.MapRoute("Defaul-Id-rent-Optional",
"Default/{id}/{rent}",
new
{
controller = "Home",
action = "Default",
id=UrlParameter.Optional,
rent=UrlParameter.Optional
}
);
Refer Routing Regression With Two Consecutive Optional Url Parameters

URL not getting read correctly by MVC 4 routing

Hi I have a URL as follows in my MVC 4 application
http://localhost/ABC/Home/DeleteApp/3000
and I have configured RouteConfig.cs as follows
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It goes to the Correct DeleteApp Controller but with the ID value Null. It shows the correct URL when I enable firebug.
What could possibly go wrong?
it debugs and come to here but the id field as null;
public void DeleteApp(string id)
{
try
{
// delete logic
}
catch (Exception e)
{
//
}
}
Possibles causes :
-> Check your "id" variable name (must be exactly the same name in the RouteConfig and in the controller action, it is case sensitive)
-> You have other routes mapped before this one that match the pattern
If you have a route mapped as {controller}/{action}/{something} and another route mapped as {controller}/{action}/{something_else}, when you call the url "Home/DeleteApp/3000", it can't tell if 3000 is "something" or "something_else" so it will take the first match.
To make this working, you have to use "Route Constraints"
http://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC
You need to do it like so:
routes.MapRoute(
name: "ABC",
url: "ABC/{controller}/{action}/{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 }
);
If you want to have url in this format http://localhost/ABC then you will have to modify the route as below by adding ABC to {controller}/{action}/{id} expression.
routes.MapRoute(
name: "Default",
url: "ABC/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

MVC: Multiple Routes for 1 Controller

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
);

How could this Routes Table be improved?

I have this RegisterRoutes method in Global.asax, can these be abbreviated with giving the same reslut? and what about their order?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Villages", // Route name
"villages", // URL with parameters
new { controller = "Villages", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"CreateVillage", // Route name
"villages/create", // URL with parameters
new { controller = "Villages", action = "Create", name = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Village", // Route name
"villages/{name}", // URL with parameters
new { controller = "Villages", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"", // Route name
"", // URL with parameters
new { controller = "Villages", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
}
You can combine first with fourth route and second with third. The defualt route might also be combined with second and third. Note that you'll probably need to use some route constraints in order to make this table work properly. You can experiment with route debugger, but I would suggest you write unit tests for your routes.
Edit:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Villages", action = "List", name = UrlParameter.Optional }, // Parameter defaults
new { action = "create|list"}
);
routes.MapRoute(
"Village", // Route name
"villages/{name}", // URL with parameters
new { controller = "Villages", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);

ASP.NET MVC Routing - "Blank" Route

Can I set up a route that will get mapped from a root-level URL like this?
http://localhost:49658/
I'm using the VS2010 built-in web server.
Attempting to set up a route with a blank or a single-slash URL string doesn't work:
routes.MapRoute(
"Default",
"/",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
It results in the error "The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.". Thanks in advance! My entire route definition is here:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"EditingTitles", // Route name
"{controller}/{action}/{startingLetter}", // URL with parameters
new { controller = "Admin", action = "Index", startingLetter = 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
);
}
What are you trying to achieve here... a URL that looks like this? http://www.acme.com/ ? Because if you are, the default route will achieve that when none of the parameters are specified.
// Default Route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = String.Empty } // Parameter defaults
);
Using ASPNET MVC5:
RouteConfig.cs file:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Homepage",
url: "",
defaults: new { controller = "Content", action = "Index" }
);
routes.MapRoute(
name: "foo",
url: "bar",
defaults: new { controller = "Content", action = "Index" }
);
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{title}",
defaults: new { controller = "Content", action = "Details", title = UrlParameter.Optional }
);
}
Plus:
If you wishes to redirect your homepage to another route automatically, like "http://www.yoursite.com/" to "http://www.yoursite.com/bar", just use the method RedirectToRoute():
public class ContentController : Controller
{
public ActionResult Index()
{
return RedirectToRoute("foo");
}
}

Resources