MVC3 Catch all path segments in URL - asp.net-mvc

I'm trying to embed a legacy Area of our MVC3 application into an iframe within the website's new root layout.
The aim is to save time and avoid Javascript and CSS styling conflicts between new and old sections of the website.
I have so far been able to conditionally redirect traffic from the old Area to a new URL within the OnActionExecuting method in the Area's controllers.
e.g. http://localhost:80/Area/Account/Profile to http://localhost:80/App/Area/Account/Profile
My trouble now is setting up a catch all route that can pass the entire URL to a specific action and controller so I can take the URL and apply it to the iFrame.
I have this in my routing but it does not pass the path to the action and if there is more than one segment in the path it does not hit the route at all:
routes.MapRoute(
"AppRedirect", // Route name
"App/{*page}", // URL with parameters
new { controller = "Home", action = "App", page = UrlParameter.Optional }
);
Is there a way a can get the route working with the full path including all segments?
Or is there a better way to embed one of the projects Areas into an iFrame within the new root layout, while maintaining a readable URL similar to the old configuration? I do not want to modify the structure or routing configuration of the old Area if it can be helped.

Do you know how many possible segments there could be in the URLs? You could set up several routes (heirarchically - most segments filtering down to one) pointing to the same controller action and do the rest in there.
routes.MapRoute(
"AppRedirect2", // Route name
"App/{seg1}/{seg2}", // URL with parameters
new { controller = "Home", action = "App", seg1 = UrlParameter.Optional, seg2 = UrlParameter.Optional }
);
routes.MapRoute(
"AppRedirect1", // Route name
"App/{seg1}", // URL with parameters
new { controller = "Home", action = "App", page = UrlParameter.Optional }
);
etc...

Related

MapRoute without affecting ActionLinks

Typically I want the default route used when generating URLs ("backwards" mapping), even if I've added alternate or legacy routes to reach an action ("forwards"):
routes.MapRoute("Legacy", "legacy/home/index", // want forward-only mapping!
new { controller = "Home", action = "Index" };
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
In this case, I'd still want the default route used when I call:
#ActionLink("Index", "Home") // generates legacy URLs ;(
In the thread: Routing legacy requests with and without querystring (issue #1), the solution is to add two MapRoute statements for each legacy route, or specify the route name in each ActionLink call.
Is there a way to define a route used only for URL -> Action mapping, not the inverse?
I can't find a way to have the route statements apply only one way, so I installed the IIS URL Rewrite module. This works to rewrite the URL transparently, or 301-redirect it in the case of legacy URLs.
It's a more maintainable solution than editing the global.asax anyway.

custom url in asp .net mvc

I am new to ASP.NET-MVC and I am trying to create a simple blog application. I want to use a custom url for the blog's details pages.
Right now the url for blog's details pages are the standard 'localhost/Blog/Details/3', but I want to actually use the url 'localhost/Blog/2012/06/blog-title', basically using 'localhost/Blog/{year}/{month}/{BlogTitle}'
I have tried looking on the internet but I do not understand how to do this and am not able to get a simple tutorial on how either.
You can create a new route in Global.asax.cs as below,
routes.MapRoute(
"Post", // route-name
"Blog/{year}/{month}/{BlogTitle}", // format
new { controller = "Books", action = "Post" }, // controller & action
new { year = #"\d{4}", month = #"\d{2}" } // constraints
);
You have to map a custom route
routes.MapRoute(
"Default", // Route name
"Blog/{action}/{month}/{BlogTitle}", // URL with parameters
new {controller ="MyController"}
);
Any url of type localhost/Blog/text/text/text will map to this route
this url will call MyController.Action(month,BlogTitle)
Make sure to put the more restrictive routes first becouse the first route that matches the url will be considered (from top to bottom)

MVC Routing: Trying to get dynamic root value working

I am trying to define dynamic sections of my site with the root url of the site. I am having some trouble defining the right MVC Route for it. Can someone please help.
My desired url will look like this: http://website.com/[dynamic-string]
But I have other standard pages like: http://website.com/about or http://website.com/faq or even just http://website.com.
My routes don't work correctly with that dynamic string. As shown below.
This is the route for the dynamic-string.
routes.MapRoute(
"CommunityName", // Route name
"{communityName}", // URL with parameters
new { controller = "Community", action = "Community", communityName = UrlParameter.Optional }
);
This is the route for all other STANDARD PAGES
routes.MapRoute(
"Default", // Route name
"{action}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
My routes just don't match up. Everything either gets diverted to one or the other route depending on which route is declared first.
There is no difference between the two routes you mention. How can MVC know which url should be mapped to communityName and which to action? Any url can match both.
You can define your standard pages as a route (before the CommunityName route) or you can catch them in your Community action, see if the name matches a function in your Home controller and then call the right action function.
I've never done this before but you might be able to create a more intelligent routehandler that looks at your controller actions, checks if the action really exists and if true selects that route.
this is beacuse the routes are effectively the same. When you declare the action route you do not state any constraints to the route, for this reason anything will be assumed to be a the action name.
If you want two routes to capture at the same level then you must constrain the action names to those that exist on your controller, this way if it does not match it will pass to the next route.
You can see an example of and advanced constraint here:
http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Custom-route-constraint-to-validate-against-a-list.aspx

Routing to an Area home page in ASP.NET MVC

I'm trying to route to the home page of an Area in MVC, e.g.
myDomain.com/myArea/Home/Index
when I use the URL:
myDomain.com/myArea
MVC appears to by trying to find a Controller called "myArea" in the root Controllers folder and thereby would route to:
myDomain.com/myArea/Index
if the Controller existed.
Again, I want:
myDomain.com/myArea
to route to:
myDomain.com/myArea/Home/Index
I figure that I should be able to do this in Global.asax without having to resort to creating a dummy controller that re-routes to the area controller, but I've drawn a blank.
One way I used to get around this was to set the namespaces property on the default route in the Global RegisterRoutes method, this seemed to resolve that problem. Had to add this restriction to some other routes where I had conflicts between Controller in the main website and the area.
var namespaces = new[] { "CompiledExperience.Website.Web.Controllers" };
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces
);

Can two different URLs be routed to the same view in ASP.NET MVC?

I am just starting to learn ASP.NET MVC and I have a situation where I have two URLs which I would like to point to the same view.
For example I could have http://some.domain/reports/daily/team1 and http://some.domain/team1/reports/daily. Could I then point them to the same view as the request is obviously the same?
The reason I am asking this is because people are forever typing the directories in the wrong order and it would be nice to pick them up rather than dump them at the 404 page.
Yes you can. Add another one of these.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
Just fill in the URL part with what you want to do or rearrange the {} parts.

Resources