I have been trying to solve this problem for a couple of days now with no success.
The case is i have rewritten an old web forms site to mvc 3 and have a bunch of legacy url:s that need to be redirected to new url:s
This is what the 2 routes look like in global.asax that should catch all requests to old url:s
routes.MapRoute(
"legacyevents",
"events/{*slug}",
new { controller = "Redirect", action = "RedirectLegacyRoute" },
new[] { "Web.Controllers" }
);
routes.MapRoute(
"legacyarticles",
"articles/{*slug}",
new { controller = "Redirect", action = "RedirectLegacyRoute" },
new[] { "Web.Controllers" }
);
The weird thing is now that when a request looks like this
events/randomevent__12.aspx
everything works well but if the E in events is upper case Events/randomevent__12.aspx
asp.net somewhere adds another events word to the url so it looks like this when it hits the RedirectController
events/events/randomevent__12.aspx
Due to lack of knowledge about SEO when i wrote the web forms app few years back alot of incoming links to the old urls have mixed casing :( so i really have to fix this issue
The route that should handle articles works as intended and does not care about the casing of the incoming request url which which makes this case so weird, since the routes are pointed to the same RedirectController.
Any help will be appreciated and if you need any additional info i will happily provide it
best regards
//K
You do know that you can use full power of regular expressions in your route matching?
See examples here.
Related
I have a working ASP.NET MVC application to which I would like to add a very simple URL which I would like to be redirected to another URL, without the Controller part of the path in it.
I.e. I want to be able to tell people to go to mySite.org/Hello and have it direct them to mySite.org/Whatever/WhateverElse?myfun=9, or whatever.
So I added a method in PublicController that redirects Hello to whatever, which works, except it requires me to tell people to go to mySite.org/PUBLIC/Hello, and we'd like to not have the extra word "public" in there.
Hours of confusing study later, I see that I could perhaps add a line in Global.asax.cs such as: routes.MapRoute(name: "Hello", url: "Public/Whatever"); ... or something... but that actually changes the way everything gets routed, and after trying various syntactical variations, nothing has seemed to just change one basic address to map to another basic address.
Is there a simple way to get mySite.org/Hello to map to another URL, without the virtual subdirectory for the public controller?
Create a custom route (it would need to be the first route in the table)
routes.MapRoute(
name: "Hello",
url: "Hello",
defaults: new { controller = "Whatever", action = "WhateverElse" }
);
then /Hello will redirect to /Whatever/WhateverElse
Edit: Sorry guys, but I wasn't seeing this behavior when I came into work the next day and couldn't reproduce it. Something else must have been going on. I was going to delete the question but you can't do that anymore. Since there aren't any upvotes anywhere, no harm done.
I'm pulling data in to a div via a jQuery ajax call. Since I'm using IE9 primarily, I need to disable output caching in jQuery using cache: false, on the ajax call. That produces a URL that looks like:
http://localhost/site/UserDetails.mvc/48d76cdd-da6f-414d-ba63-f24708d351ff?_=1315347866786
What I actually want is:
http://localhost/site/UserDetails.mvc/48d76cdd-da6f-414d-ba63-f24708d351ff
Note the ?_=1315 toward the end of the first one. I'm pretty sure that's a timestamp that jQuery is adding to prevent output caching. This is breaking my mvc routing, which is expecting a single ID field at the end of the route:
routes.MapRoute(
"DefaultNoAction", // Route name
"{controller}.mvc/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
So I'm getting a 404 for the URL that ends with the timestamp. I'm pretty new to MVC and I don't know how to tell the router that any url parameter that is named _ should be ignored. How would I do this?
Take a look at the ASP.NET MVC: url routing vs querystring thread, where discussed how to handle this case.
This is breaking my mvc routing, which is expecting a single ID field at the end of the route
No, this is not breaking anything on your routes. Query string parameters are not part of the routes. They are ignored.
So I have an ActionName like so:
[ActionName("Chicago-Bears")]
public ActionResult ChicagoBears() {
return View();
}
Google has this indexed as: http://www.example.com/Teams/ChicagoBears
I'm stuck using IIS6 and have no access to IIS myself.
Of course now, it has a hyphen in it. So, Google will show a 404 if someone clicks on that link.
How do I setup a 301 redirect in this instance? I can't create another method called ChicagoBears(), so...
Thanks guys.
Create a route for Teams/ChicagoBears that points to an action that gives a permanent redirect.
In Global.asax...
routes.MapRoute("ChicagoBearsRedirect",
"Teams/ChicagoBears",
new { controller = "Teams", action = "RedirectChicagoBears" }
);
In TeamsController...
public ActionResult RedirectChicagoBears()
{
return RedirectToActionPermanent("Chicago-Bears");
}
The URL Re-Write Module is your friend. Learn it, live it, love it...
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/
I used it extensively when I migrated from DasBlog to WordPress. All my old blog URLs are re-directed using 301's to the new ones. Highly recommended.
UPDATE: There are URL re-writers for IIS6. A quick google search turned up:
http://www.isapirewrite.com/
http://urlrewriter.net/
(Found via http://forums.iis.net/t/1160436.aspx.)
UPDATE: This blog I referenced seems to no longer be available so I updated the link to reference the internet archive version.
Check out this blog post for a great solution*: https://web.archive.org/web/20160528185929/http://www.eworldui.net/blog/post/2008/04/25/ASPNET-MVC-Legacy-Url-Routing.aspx
Essentially what he is doing is creating a reusable class that can be used for multiple routes, and they just issue a permanent redirect to the specified Action method.
**Note: This is not my blog, but one that I simply came across.*
Little late to the party on this one, but I wrote a blog post about permanent redirects for legacy routes that allows this -
routes.MapLegacyRoute(
null,
"Teams/ChicagoBears",
new { controller = "Teams", action = "ChicagoBears", area="" }
);
The Location to redirect to is generated using the route values using Url.Action, so as long as you have a route in the RouteTable that matches the route values, the 301 redirect will work as intended. In your example, the generated URL should be http://www.example.com/Teams/Chicago-Bears when the URL pattern matches "Teams/ChicagoBears".
I won't repeat the code here as there's quite a bit and it's on the blog
I'm getting a strange error on my webserver for seemingly every file but the .aspx files.
Here is an example. Just replace '/robots.txt' with any .jpg name or .gif or whatever and you'll get the idea:
The controller for path '/robots.txt'
was not found or does not implement
IController.
I'm sure it's something to do with how I've setup routing but I'm not sure what exactly I need to do about it.
Also, this is a mixed MVC and WebForms site, if that makes a difference.
You can ignore robots.txt and all the aspx pages in your routing.
routes.IgnoreRoute("{*allaspx}", new {allaspx=#".*\.aspx(/.*)?"});
routes.IgnoreRoute("{*robotstxt}", new {robotstxt=#"(.*/)?robots.txt(/.*)?"});
You might want to ignore the favicon too.
routes.IgnoreRoute("{*favicon}", new {favicon=#"(.*/)?favicon.ico(/.*)?"});
You can adjust the regular expression to exclude paths.
Haacked from the source.
The ignore route given above didn't work for me but I found a similar one that did:
routes.IgnoreRoute("{*staticfile}", new { staticfile = #".*\.(css|js|gif|jpg)(/.*)?" });
This error could also happen if inside a view in your area, you use the Html.Action helper. This helper will always use the area as a prepend, unless you specifically tell it not to. E.g.,
#Html.Action("Main", "Navigation", new { area = string.Empty })
I found another solution too... While I don't think I'll use it, it's worth showing here in the answers:
The following should (in theory) ignore looking for controllers for anything with a '.' in it.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
new { controller = #"[^\.]*" } // Parameter contraints.
);
Do you still have:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
... in your Global.asax.cs?
MVC puts it there by default, and it's supposed to handle this.
If you do, then the problem may be how you're mixing MVC and WebForms.
I encountered this error when I request resources that did not exist.
Specifically, I was requesting a custom IE css file:
<!--[if lt IE 8]>#Styles.Render("~/Content/ie7.css")<![endif]-->
(These are condition comments, interpreted by IE)
However, the actual resource existed on ~/Content/ie/ie7.css.
So, without any modifications to the routing, the error was solved by using the correct url of the resource.
I am building a simple web site for a client and this is my first time with ASP.Net Mvc.
For the production i need to use MVC 1.0 and the most efficient way to saparate Admin logic from the rest of this site is using Areas.
The fact that i couldnt use MVC 2, so i have used the Haacks area prototype and everything was ok.
I want to write a custom routing for paging results but i couldnt get it done.
routes.MapAreas("{controller}/{action}/{id}",
"Adore.Web",
new[] { "Admin" });
//my custom routing
routes.MapRoute(
"PagingServices",
"Admin/Services/{pageNumber}",
new { area = "Admin", controller = "Services", action = "Index" });
routes.MapRootArea("{controller}/{action}/{id}",
"Adore.Web",
new { controller = "Home", action = "Index", id = "" });
As you see above i am trying to get this "Admin/Services/1" but couldnt figure it out.
How can i do it, thanks in advance !
Have you tried Phil Haack's Routing Debugger? :) http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
That might help.
Also, it looks like the problem might be that your first route is going to match anything starting with /Admin. Try moving your custom route to the top since routes are evaluated in order.