Change controller and action name in ASP.NET MVC routing - asp.net-mvc-3

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

Related

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

Handling Routes in 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*"
}
);

Routing - Area Controller/View with parameter

Super simple MVC site with an Area to handle mobile devices. All of my Area routing works fine with the exception of a view that expects a parameter.
In the "normal" site I have a view video page that expects a parameter.
mysite.com/Video/123456
This works perfectly. After fighting this for a bit in my Area for the mobile content I have even gone down to using the exact same code/markup in my Controller and View. So I would expect that the following URL:
mysite.com/Mobile/Video/123456
Would resolve properly. It doesn't. I get a 404 (not found). If I take the parameter off:
mysite.com/Mobile/Video
It resolves properly.
I am sure this must be something I am doing wrong in the routing. Below is the appropriate section from my global.asax. Any help would be appreciated.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Video", // Route name
"Video/{id}", // URL with parameters
new { controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Controllers.VideoController" }
);
routes.MapRoute(
"NewsItem", // Route name
"NewsItem/{id}", // URL with parameters
new { controller = "NewsItem", action = "Index", id = 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
new string[] { "mysite.Controllers.HomeController" }
);
routes.MapRoute(
"Mobile", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { area = "Mobile", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Areas.Mobile.Controllers.HomeController" }
);
routes.MapRoute(
"Mobile/Video", // Route name
"Mobile/Video/{id}", // URL with parameters
new { area = "Mobile", controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Areas.Mobile.Controllers.VideoController" }
);
}
SteveInTN, you cannot have the same registration in both, Global.asax and MobileAreaRegistration.cs.
You only need to have Mobile Registration on MobileAreaRegistration.cs and call AreaRegistration.RegisterAllAreas() in Application_Start before RegisterRoutes(RouteTable.Routes).
If you want url like mysite.com/Mobile/Video/123456:
The mobile route registration should be in the format {controller} / {id}, like video route.
Registration in Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Video", // Route name
"Video/{id}", // URL with parameters
new { controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Controllers.VideoController" }
);
//newsitem route
}
Registration on MobileAreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Mobile_default",
"Mobile/{controller}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Looks like your Route name should not contain / since it may conflict with routing? When I do routing I make sure the names are unique and use underscores to represent separators like so : text_text. Not sure if this will work, worth a try though.

Unable to understand the Asp.net MVC routing

I have added the following route to my global.asax file :-
routes.MapRoute(
"Admin_Route",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "PriceCompare.Admin.Controllers" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "PriceCompare.Controllers" }
);
The admin controllers i.e. ManageCatsController, ManageBrandsController, etc. reside in PriceCompare.Admin.Controllers namespace and other general controllers reside in PriceCompare.Controllers namespace.
The problem is that i am able to visit all the controllers by adding Admin/ in front of them, irrespective of whether they are in PriceCompare.Admin.Controllers namespace.
Also, I am able to visit admin controllers directly without prefixing Admin/.
Why is this happening. Am i misunderstanding the routing behaviour.
You need to register your Admin area.
When I have registered routes for areas I've always done it like this:
Inside App/Areas/Admin folder create an AdminAreaRegistration.cs file with this in it...
using System.Web.Mvc;
namespace AppName.Areas.Admin
{
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin",
"Admin/{controller}/{action}/{id}",
new { controller="Home", action = "Index", id = UrlParameter.Optional },
new string[] { "AppName.Areas.Admin.Controllers" }
);
}
}
}
Now do this in Global.asax:
protected void Application_Start()
{
// Add this next line
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
// Add any other stuff (like IoC or whatever)
}
And only register your normal routes in RegisterRoutes in Global.asax, like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "AppName.Controllers" }
);
}
I would also recommend keeping the Area part inside your Admin namespace (so calling it PriceCompare.Areas.Admin.Controller in your case) as it will make life a lot easier later on when.
Please try this and let me know if it works :-)

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