ASp.net mvc Url Routing Problem - asp.net-mvc

I have a route setting for Profile controller
In order to view the profile page like http://localhost/Profile/MyUserName
routes.MapRoute("Profile", "Profile/{userName}", new { controller = "Profile", action = "Index", userName = "" });
These works fine.
My problem is that because the profile controller has many actions
Like... Profile/Edit,
Profile/Save,
Profile/Updates,
Profile/etc.... so on..
All of these actions got hit in the route "Profile/{userName}".
In order to fix it i have to map all of these actions in the route table which is very ugly bec. i only want to map the route "Profile/{username}"
Is there a way that i can map only 1 route to profile controller and the rest i dont care about their url format?
im using mvc 1

Instead Profile/{userName} I'm using Profile/View/{userName}. In this way I avoid case, when username is Edit or Save.

Related

How do I tell what is part of a route is the controller and what is just a sub folder?

When looking at an MVC page and looking at the form action I can see it is set to:
/admin/WikiAdmin/edit/
So I spent my time looking for a controller called admin. I looked in the routes in the global and nothing was there.
Eventually I found that this url actually maps to the WikiAdmin controller which is confusing. Do does this mean you can have controllers in sub-folders? How does the app know not to forward the request to the admin controller and to actually send it to the WikiAdmin controller?
The admin part of the url is called area. You could read more about areas in this article. And a video here. Basically areas allow you to group multiple controllers sharing some common functionality on the site.
Yes, you can have controllers in sub-folders. With routing it could be a lot of possible URLs.
For example, if you have a route registered as below:
routes.MapRoute(
"admin1",
"admin/{controller}/{action}/",
new { controller = "WikiAdmin", action = "Index"}
);
The url can be /admin/WikiAdmin/Index/ or /admin/WikiAdmin/Edit/ or something else that matches the route. (Assume that there is an Edit action in WikiAdmin controller)
More example, if you have a route registered as below:
routes.MapRoute(
"admin2",
"account/{action}/", //no controller specified in url
new { controller = "WikiAdmin", action = "Index"}
);
Then the url can be /account/Index/ or /account/Edit/ or even /account/. (Because default controller is WikiAdmin and default action is Index)

MVC 3 routing issue

Well, technically it is an ASP.net routing question but since I am using MVC 3 here we go.
I need to setup a route as follows:
http://www.mysite.com/profile/1 where 1 is the userid, however I want to hide the userid param in the query string because it is just plain ugly.
Controller is ProfileController
Action is Index
parameter is userid.
I can't seem to figure this out. I am probably thinking about it too much...
Any help would be ultra cool.
The route should be nice and simple. It needs to come before your default route handler.
routes.MapRoute(
"Profile", // Route name
"profile/{userId}", // URL with parameters
new { controller = "Profile", action = "Index" } // Parameter defaults
);

Twitter like url routing in asp.net mvc?

I ve seen in twitter, i can get a user view page by just typing in the url say http://twitter.com/pandiyachendur. How to do the same with asp.net mvc? I dont know how twitter does it?
You need to be careful about the order in which you declare your routes. Since there is no common element to a /{username} URL, you need to declare it as the last 'catch-all' route, after all of your specific routes.
RouteTable.Routes.MapRoute(null, "LogIn", new { controller = "Account", action = "LogIn" });
RouteTable.Routes.MapRoute(null, "LogOut", new { controller = "Account", action = "LogOut" });
// ... other routes go here ...
// Final catch-all route to map /{username} to the Account.Details action.
RouteTable.Routes.MapRoute(null, "{id}", new { controller = "Account", action = "Details" });
It's also worth remembering that you need to extend your validation on usernames to prevent people from choosing names that conflict with the specific routes (e.g. LogIn).
I imagine that they have some regular exception that checks the request to see if it matches something that could be a user's profile and then push that request to an appropriate controller action.
They'd likely might list first all of the exceptions are static routes, like "/invitations", and then pass everything else to a default controller action that attempts to display a user's page.

Avoiding the Controller with Routing Rules in ASP.NET MVC

I've created a website with ASP.NET MVC. I have a number of static pages that I am currently serving through a single controller called Home. This creates some rather ugly URLs.
example.com/Home/About
example.com/Home/ContactUs
example.com/Home/Features
You get the idea. I'd rather not have to create a controller for each one of these as the actions simply call the View with no model being passed in.
Is there a way to write a routing rule that will remove the controller from the URL? I'd like it to look like:
example.com/About
example.com/ContactUs
example.com/Features
If not, how is this situation normally handled? I imagine I'm not the first person to run in to this.
Here's what I've done previously, using a constraint to make sure the shortcuts don't conflict with other routing rules:
routes.MapRoute(
"HomeShortcuts",
"{action}",
new { controller = "Home", action = "Index" },
new { action = "Index|About|ContactUs|Features" }
);
Add defaults for the controller names in the new statement. You don't have to have {controller} in the url.

ASP.NET MVC global redirect

Most of our projects are short term and of a promotional nature. As a result, our clients often want to put up some sort of "end of program" or "expired" page when the promotion is over. How do I do a blanket redirect of all controller actions to one specific controller action without modifying each controller and its methods? Is this even possible?
Ideally, in pseudocode, I'd like to be able to do something like this:
// somewhere in global.asax
if (current_action_url != desired_action_url)
redirect to desired_action_url
I tried doing simple string matching on the URL:
if (!Request.Url.AbsolutePath.ToLower().EndsWith("path/to/desired/page"))
Response.Redirect("path/to/desired/page");
However, since I'm still using IE 6 and have to use the wildcard hack, IE was redirecting all requests to the page (even images and stylesheets) which messes things up pretty badly.
How about using routes? Define routes that are for valid promos, the catch all can go to the generic "promo expired" page
routes.MapRoute(
"PromoStillGoing",
"path/to/PromoStillGoing/{action}",
new { controller = "PromoStillGoing", action = "Index" });
routes.MapRoute("Catch All", "{*path}", new { controller = "ExpiredPromos", action = "Index" });
The above will do one page for all expired promos, not sure by your question if that is what you want.
If you want to have one expired page per promo, then in the routes you can "ignore" the action in the requested url like
routes.MapRoute(
"ExpiredPromoName",
"path/to/PromoName/",
new { controller = "PromoName", action = "Index" });
Now anything under /path/to/PromoName will use the Index action of the PromoNameController

Resources