Handling Routes in ASP.NET MVC 3 - asp.net-mvc-3

I am trying to get my routes setup for an Area and I have the following URLs that I need to allow:
http://localhost/Review/Setup
http://localhost/Review/Setup/65
http://localhost/Review/Setup/_AjaxGetMember?ReviewId=53
Area = Review | Controller = Setup | Action = Index
Thus for the URLS, they should go to:
http://localhost/Review/Setup/Index
http://localhost/Review/Setup/Index/65
http://localhost/Review/Setup/_AjaxGetMember?ReviewId=53
Here is the route that is currently registered for the area.
context.MapRoute(
"Review_default",
"Review/{controller}/{action}/{id}",
new
{
action = "Index",
id = UrlParameter.Optional
}
);
Right now, 1 and 3 in my first list work but 2 does not without putting Index into the URL. What else can I add to make these URLs work so that Index does not have to be added or show up in the URL?
Thank you.

I did not test it but that's how I would try it:
context.MapRoute(
"Review_setup_directId",
"Review/Setup/{id}",
new
{
controller = "Setup",
action = "Index",
id = UrlParameter.Optional
}
);

Solved it. I needed to add more routes. There may be a more elegant way to do this but I just tested and it works.
context.MapRoute(
"Review_default",
"Review/{controller}/{id}",
new
{
action = "Index",
id = UrlParameter.Optional
},
new
{
id = #"\d*"
}
);
context.MapRoute(
"Review_default2",
"Review/{controller}/{action}/{id}",
new
{
action = "Index",
id = UrlParameter.Optional
},
new
{
action = #"[A-Za-z]+",
id = #"\d*"
}
);
context.MapRoute(
"Review_default3",
"Review/{controller}/{action}/{id}",
new
{
action = "Index",
id = UrlParameter.Optional
},
new
{
action = #"_[A-Za-z]+",
id = #"\d*"
}
);

Related

MVC3 Route Using Parameter Name in URL

I have these routes:
routes.MapRoute("ListPage", "{controller}/{action}/{pn}/{ps}", new { controller = "home", action = "index", pn = 1, ps = 10 });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "home", action = "index", id = UrlParameter.Optional });
Which allows me to have URLs like:
/foo/bar/1/10
to control lists of foos on a page (page 1, with 10 items a page). Hooray!
However, the following gives a 404:
/foo/bar/1
Using Url.Action("bar", "foo", new { id = 1}) gives the URL
/foo/bar?id=1
Which then matches correctly to the action signature
public ActionResult Bar(int id) { //stuff }
My thinking is that the first route in the table would not match, as both {pn} and {ps} are required.
So it drops to the second route, which should then match the parameter as {id}.
Obviously my thinking is not correct!
Question is: why is the route not matching without the parameter name?
Just try with interchanging routes postition
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "home", action = "index", id = UrlParameter.Optional });
routes.MapRoute("ListPage", "{controller}/{action}/{pn}/{ps}", new { controller = "home", action = "index", pn = 1, ps = 10 });

Trouble with global.asax routing

I have some links in my webapp that looks like this:
localhost:12345/?something=1
localhost:12345/?something=2
localhost:12345/?something=3
localhost:12345/?something=4
each number at the end is an id that i need to pass to my controller to display information related to it.
I know I need to create a new routes.MapRoute in my global.asax page, but I am not really quite sure how to go about it. I tried this:
routes.MapRoute(
"Id", // Route name
"{controller}/{action}/{*Id}", // URL with parameters
new { controller = "Home", action = "Id", Id = "" } // Parameter defaults
);
---EDIT---
I am only successful getting each individual like to display by doing the following:
routes.MapRoute(
"IdRoute", // Route name
"{Id}", // URL with parameters
new { controller = "Home", action = "Index", id = 1 } // Parameter defaults
);
This does work, however, this only works for one id (specifically 1). I am not quite sure how to go about this, but i need i need:
localhost:12345/?something=1
to display the information for id 1,
localhost:12345/?something=2
to display the information for id 2,
localhost:12345/?something=3
to display the information for id 3.
I there are going to be hundreds of ids so hard coding something in would not be a convenient option. I have had no luck so far. Any help would be much appreciated! Thanks!
routes.MapRouteWithName(
"RootName",
"{id}",
new { controller = "Home", action = "Index", id = 1 });
This will produce links like this localhost/1
If you want this kind of links localhost/?id= 1
Then :
routes.MapRouteWithName(
"RootName",
String.Empty,
new { controller = "Home", action = "Index"});
public ActionResult Index(int id)
{
//do something with id, make query to database whatever
// u usually have model class so you would fill model with your data
var model = new YourModel();
//...
return View("Index", model);
}
If you have following Action in, say, HomeController:
public ActionResult SomeAction(int Id)
{
return View()
}
You may use any of following routes:
//* For Id = 3 this will return path "Home/SomeAction/3"
routes.MapRoute(
name: "First",
url: "{controller}/{action}/{Id}",
defaults: new { controller = "Home", action = "SomeAction", Id= UrlParameter.Optional}
);
//* For Id = 3 this will return path "SomeAction/3"
routes.MapRoute(
name: "First",
url: "{action}/{Id}",
defaults: new { controller = "Home", action = "SomeAction", Id= UrlParameter.Optional}
);
//* For Id = 3 this will return path "Home/SomeAction(3)"
routes.MapRoute(
name: "First",
url: "{controller}/{action}({Id})",
defaults: new { controller = "Home", action = "SomeAction", Id= UrlParameter.Optional}
);
//* For Id = 3 this will return path "LadyGaga/SomeAction/3"
routes.MapRoute(
name: "First",
url: "LadyGaga/{action}/{Id}",
defaults: new { controller = "Home", action = "SomeAction", Id= UrlParameter.Optional}
);

Change controller and action name in ASP.NET MVC routing

I created an areas -> Admin.
In my register area, I have:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
I changed it to:
context.MapRoute(
"jojo",
"jojo/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
Now if you type in a URL, xxx/jojo/AdminHome/Index, it works perfectly, but how can I change the controller and action names until the user can not finds that it's going to the admin area. Notice that I do not want change my controller name to jojo, for example.
Is it possible?
You can do this:
context.MapRoute(
"jojo",
"jojo/jojo/{action}/{id}",
new { controller="RealController", action = "Index", id = UrlParameter.Optional }
);

MVC Routing problem, 404 and unmatched routes

I'm trying to setup a routing scheme in MVC3 that matches against a legacy (SP 2007) system. These are the routes I've setup:
routes.MapRoute("administration",
"Administration/{action}/{id}",
new { controller = "Administration", action = "Index", id = UrlParameter.Optional });
routes.MapRoute("workOrderSearch",
"WorkOrderSearch",
new {controller = "Home", action = "WorkOrderSearch"});
routes.MapRoute("customers",
"{customerNumber}/{action}",
new {controller = "customer", action = "Index"},
new {customerNumber = #"\d*"});
routes.MapRoute("graphicNames",
"{customerNumber}/{graphicNameId}/{action}/{id}",
new {controller="GraphicName", action="Index", id=UrlParameter.Optional},
new {customerNumber = #"\d*",graphicNameId = #"\d*", action=#"\w*"});
routes.MapRoute("workOrders",
"{customerNumber}/{graphicNameId}/{graphicNumber}/WorkOrder/{action}/{id}",
new { controller = "WorkOrder", action = "Index", id = UrlParameter.Optional },
new { graphicNameId = #"\d*", graphicNumber = #"\d*-\d*" });
routes.MapRoute("graphics",
"{customerNumber}/{graphicNameId}/{graphicNumber}/{action}",
new { controller = "Graphic", action = "Index", id = UrlParameter.Optional },
new { graphicNameId = #"\d*", graphicNumber = #"\d*-\d*" });
routes.MapRoute("Default", "", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
It mostly works just fine. However, when trying to hit the "graphicNames" route, I run into a problem. If I use this url:
http://localhost:1234/1234/321/Index
it works fine and I get to the Index action on the GraphicName controller. However, if I do this:
http://localhost:1234/1234/321
I get a 404.
All other routes appear to work as expected.
Edit: The solution was to add a constraint to the customer's route so that actions were only 'action=#"[A-Za-z]*"
Above you have:
routes.MapRoute("graphicNames",
"{customerNumber}/{graphicNameId}/{action}/{id}",
new {controller="GraphicName", action="Index", id=UrlParameter.Optional},
new {customerNumber = #"\d*",graphicNameId = #"\d*", action=#"\w*"});
however before that you have
routes.MapRoute("customers",
"{customerNumber}/{action}",
new {controller = "customer", action = "Index"},
new {customerNumber = #"\d*"});
which based on your url with only TWO parameters
/1234/321
will match the customers route first. Either add a route constraint that action must be alpha only, or move this beneath your graphicNames route, since order is very important in route matching.

Routing without the controller name

I have a few controllers ,
One let say computer controller
It has action for laptop, desktop gadgets
i would like to have pages name :www.MyDomain/laptop (and so on )
and one let say electronic controller
it has action TV, DVD, (and so on )
i would like to have pages name :www.MyDomain/TV (and so on )
Without specifies the controller?
I don’t understand what happen to my question before I can't edit
(I hope the admin will delete it )
You could do this by specifying route constraints:
routes.MapRoute(
"Computers",
"{name}",
new { controller = "Computers", action = "Index", name = UrlParameter.Optional },
new { page = "laptop|desktop" }
);
routes.MapRoute(
"Gadgets",
"{name}",
new { controller = "Electronic", action = "Index", name = UrlParameter.Optional },
new { page = "tv|dvd" }
);
Now /laptop and /desktop will be routed to the Index action of the ComputersController and /tv and /dvd will be routed to the Index action of the GadgetsController.
In your Global.asax.cs:
routes.MapRoute(
"ViewLaptop", // Route name
"/laptop", // URL with parameters
new { controller = "Computers", action = "Laptop" } // Parameter defaults
);
This should do it.

Resources