Bidirectional Route in ASP.NET Routing - asp.net

Okay, here's the deal:
I have an aspx page that looks like mysite.com/UnitDetails.aspx?UnitID=123
Right now I've setup my routing to be able to take in something like mysite.com/My/Path/123 and redirect it to UnitDetails.aspx. This works great.
However, what I'd like to be able to also do is redirect the user to the clean url if they type in the aspx page. For example, if you have mysite.com/UnitDetails.aspx?UnitID=123 bookmarked, I'd like it to show up as mysite.com/My/Path/123.
How can I accomplish this? Here's what I have right now in my routing:
RouteTable.Routes.MapPageRoute("unit_details", "{area}/{property}/{unit_id}", "~/UnitDetails.aspx")

Okay, I figured it out!
Just needed to add this into my UnitDetails.aspx code behind in the Page_Load:
//issue a 301 if legacy route requested to boost SEO
if (Request.Url.AbsolutePath.Contains("UnitDetails.aspx"))
{
var unit = BeachGuide.Models.Unit.GetById(Convert.ToInt32(Request.Params["UnitID"]));
Response.RedirectPermanent(unit.relative_url);
}
So now if they request the new url, they won't get caught in an infinite loop. If they request the old url, we have a base case that redirects them to the new url via a 301 redirect. This will help search engines know to use the new route as the canonical url.

Related

How do I redirect a route to another route in ASP.NET web forms?

I have routes like these:
routes.MapPageRoute("Survey", "Survey", "~/Survey/Survey.aspx")
routes.MapPageRoute("Letters", "About/Letters", "~/Pages/Letters/Letters.aspx")
How can I redirect a url like this: /Surveys to the 'Survey' route? So that when the user goes to /surveys it redirects to /Survey. (URLs for the sake of argument)
I'd prefer it if I didn't have to place redirect code in the ASPX file itself, and rather just have the code in the route rule, just keeps it simple and centralized.
Thanks
Luke
You can use something like this
Response.RedirectToRoute("Survey");
the parameter "Survey" is routeName you defined in Global.asax using MapPageRoute. RedirectToRoute also has other overloads that allow you to pass route parameters if required
If you really don't want to create a physical file for /Surveys then you can use IIS url rewriting capabilities to redirect all requests from /Surveys to /Survey.
Check out this link to see how to do it in IIS.

What's the trouble with my ASP.NET MVC route?

I have some routes in my ASP.NET MVC application that handle redirecting of old urls. The URL I'm redirecting is:
contentSpanishContentList.aspx
Here's the route:
routes.MapRoute("RedirectLegacyContent1",
"content{contentUri}.aspx",
new { controller = "Redirect", action = "Content", contentUri = string.Empty, contentId = 0 });
The problem is it comes up as not found. I figured out that the problem is (in bold) contentSpanish*Content*List.aspx. What should I do to make this route work with this case?
Two solutions
Rename your pages to not include the same constant string (in your case it's the word content).
Write a custom route that's able to parse your requests - all you'll have to override is the GetRouteData method. And if you're planning to only use this route for incoming requests (not generating any URLs in your views to point to any of these pages ie. using Url.Action or Html.ActionLink) then the easiest way would be to generate something like a RegExRoute which would be easy to write and also easy to resolve these kind of requests.
The fist one is simple, the second is universal.
Use Fiddler to look at what's happening. Is the 404 happening on the first request? Or is it happening after the redirect?
Install the RouteDebugger package and see what it tells you.

Response.Redirect ReturnUrl

I'm trying to use Response.Redirect on an ASP.NET page that uses routing and membership -- in order to redirect non logged in users to the main page. This is a page that should sometimes be viewable to anonymous users based on content. When I redirect to the login page, the browser fills the returnurl with invalid content.
The question is how do I remove the ReturnURL before sending the user "off"? Or how do I fix it so that it includes the proper link?
The page is in a different directory. It redirects properly but sets the ReturnURL to an invalid path.
The ReturnUrl is just set to the folder. Not to the page with query params nor do the route.
UPDATE
Okay where is what it is doing
I have a folder Actions. I'm redirecting to Login.aspx. It redirects to login.aspx but then sets the returnurl to Actions/Login.aspx which is absolutely wrong.
It is wrong on 2 accounts:
The login.aspx doens't exist in the actions folder
It creates a recursive redirect
Update Fixed Partially
Okay, it was because I was in a different folder and not redirecting to the proper page.
I had "login.aspx" instead of "../login.aspx"
However, it is not setting the returnurl to the routed path. It is stripping the returnurl. I may have decided to do this as a design decision though, not 100% sure.
I have not understood the nitty gritty of the issue you are facing, but I guess you might have missed to apply Server.UrlEncode to return url. Please have a look at what Server.UrlEncode does at
http://msdn.microsoft.com/en-us/library/ms525738(v=vs.90).aspx
Below is the piece of code which shows how specify Return Url using Server.UrlEncode
Response.Redirect(FormsAuthentication.LoginUrl + "?ReturnUrl=" +
Server.UrlEncode(Request.Url.ToString()))

Url rewrite question

When you have done your url rewrite for a url what happens when you use your code behind file and use request.querystring when there aren't any cos you url has been re-written. I haven't implemented url re-writing yet and want to.
Well, not sure if I understand your question...
If you use url rewriting the user sees on the browser a url like this one
www.yourdomain.com/category1/product1
While you see on the code behind something like this
www.yourdomain.com?cat=1&prod=1
The friendly url is converted to the "ulgy" format before reaching your page logic, so the querystring will be there and ready to be processed.

ASP.Net MVC - Trapping certain URL's to do 301 Redirect

I am moving from an old site design to a new design with new URL's.
All previous page names were static files called PageXX.html, PageX.html, Index.html - where X is a number.
My site is now dynamic but I want to trap for those 3 incoming url's and then try and redirect to a certain new page (301 Redirect) else send them to the home page.
Do I do all of this in Global.asax or do I just trap those Url's in Global.asax and then route it to a Action and do a 301 Redirect in the Action?
Any code examples would help a lot!
Thanks
EDIT: I think what needs to be done is trap the routes in Global.asax and then send them to a Action which will work out where to send the user ie. a similar page on the new site else I will send to the home page.
That's right, just do it in your routes configuration (usually in global.asax). You can set these up as static special cases.
routes.MapRoute("Page3",
"SomeURL/Page3.html",
new {
controller = "SomeController",
action = "SomeAction",
page = "2"
});
For PageXX.html, PageX.html, Index.html pages you can do regular expression based matching too. That will allow you to maintain the whole thing with a single route mapping.

Resources