ASP.NET MVC - 301 Redirect - SEO issue - asp.net-mvc

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

Related

Is there a simple way to map a single URL to another URL in ASP.NET MVC?

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

What is the difference between Redirect and RedirectToAction in ASP.NET MVC?

What is the difference between Redirect and RedirectToAction other than their return type?
When do we use each? Explanation with any real life scenario would help me greatly.
I was looking at Confusion between Redirect and RedirectToAction, but, to me, it looks like the answer is more specific towards handling id parameter and returning proper view.
RedirectToAction lets you construct a redirect url to a specific action/controller in your application, that is, it'll use the route table to generate the correct URL.
Redirect requires that you provide a full URL to redirect to.
If you have an action Index on controller Home with parameter Id:
You can use RedirectToAction("Index", "Home", new { id = 5 }) which will generate the URL for you based on your route table.
You can use Redirect but must construct the URL yourself, so you pass Redirect("/Home/Index/5") or however your route table works.
You can't redirect to google.com (an external URL) using RedirectToAction, you must use Redirect.
RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table.
Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
Best Practices: Use RedirectToAction for anything dealing with your application actions/controllers. If you use Redirect and provide the URL, you'll need to modify those URLs explicitly when your route table changes.

return url is not working on Live server vs development server

I have an MVC3 Razor view which has a link that requires user login and its html is like this:
<a href="Home/Login?returnUrl=/myprojname/Product/Index">
and then an action method Login in homecontroller and a view. Which posts values to Logon action method.
inside the post action method I check if returnurl is not null then redirect to that URL. On local system it returns to this address
http://localhost:1235/myProjname/Product/Index
and works fine. But when I upload it to server (IIS 7), It returns to
http://domainname/myProjname/Product/Index
and gives 404 because there is no 'myProjname' there, If I remove the 'myprojname', it works with this url on live server
http://domainname/Product/Index
but for that I need to change href like this
<a href="Home/Login?returnUrl=/Product/Index">
In that case, It doesn't work with local system with URL
http://localhost:1235/Product/Index
Please suggest solution
Are you serious?
Okay, we'll i'll play along.
The solution is to create links properly, not hard code them.
#Html.ActionLink("Login", "Home", new { returnUrl = Url.Action("Index", "Product") })
On a side note, you probably don't need to pass returnUrl to the action. Pull it back from the Request.UrlReferrer.AbsoluteUri in the action method itself.
Not meaning to sound rude, but I strongly suggest you head over to http://www.asp.net/mvc and have a read/watch before proceeding any further.
using Url.Action and Url.Content helper methods will generate URLs relative to the application root, and avoid these problems.
Also, #Href("~/") will generate the full path to the application root (from a view).

Mvc 3 redirecting routes behaving weirdly

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.

aspx url to mvc controller action

I have an aspx page Test.aspx. It handles requests like
Test.aspx?First=value1&Second=value2&third=value3
How can I use routing to redirect this url to
TestController/MyAction?First=value1&Second=value2&third=value3
I know I can create an aspx and perform redirect in it`s page load. But seems ugly and I think it can be done with some custom route.
What I`ve tried was: this solution
but it didnt work for me.
I remember, that Test.aspx should not be on a disk. I don`t have it, and routing is still not working. Have no idea on what can cause this issue.
Have you tried adding a route like the following:
routes.MapRoute(
"Test",
"Test.aspx",
new { controller = "TestController", action = "Show" }
);
Just remember that the route will not work if the Test.aspx file is still on disk.
Also, ideally you would want to have a permanent redirect so the search engine links etc get updated to point to your new urls.

Resources