1

I am converting an old ASP.NET WebForms app to ASP.NET MVC 4. Everything is fine, except that I have a need to maintain backward compatibility with a specific URL. I found this great post on using UrlRewrite, but sadly that isn't something I can count on (this app gets deployed to lots of servers). At the bottom, he mentions using routing if you only have a small set of URLs to deal with, but doesn't provide any example.

Since I only have one url to deal with, I think routing would be the simple approach, but I've never dealt with anything except the default route /Controller/Action/{id} so I am looking for a solution that

  1. Has no external dependencies
  2. Will work on old crappy browsers
  3. Doesn't matter if my app knows about this old url or not

The Old URI

https://www.mysite.com/default.aspx?parm1=p1&parm2=p2&etc=soforth

The New URI

https://www.mysite.com/Home/Index/?parm1=p1&parm2=p2&etc=soforth

Background: this app gets deployed to lots of servers at different locations. There are other apps (that I cannot update) that display the "Old URI" in a web-browser control, so I need them to continue to work after the app is updated to asp.net mvc.

3
  • Why don't you add a rewrite rule in the web.config of your project as shown in your link? Commented Jan 18, 2013 at 0:33
  • have you looked into MapPageRoute() ? Commented Jan 18, 2013 at 5:55
  • @cheesemacfly I cannot count on the url rewrite module being active on all servers I will be deploying to. Commented Jan 18, 2013 at 15:36

2 Answers 2

4

Something like following should work (untested, may need to make this route to be one of the first):

routes.MapRoute(
   "legacyDefaultPage",
   "default.aspx",
   new {Controller = "Legacy", Action="Default"});

class LegacyController {
  ActionResult Default (string param1,...){}
}
Sign up to request clarification or add additional context in comments.

Comments

0

Something that you could try is to create an httpModule this will get executed just before hit any route to in your MVC app.

In your http module you can perform a 301 or 302 redirection if seo matters doing that will give you more flexibility to transform all the parameters from your legacy to your new app.

public class RecirectionModule :IHttpModule{
    public void Init(HttpApplication context)
    {
        _context = context;
        context.BeginRequest += OnBeginRequest;
    }

    public void OnBeginRequest(object sender, EventArgs e)
    {
        string currentUrl = HttpContext.Current.Request.Url.AbsoluteUri;
        string fileExtention = Path.GetExtension(currentUrl);
        string[] fileList= new[]{".jpg",".css",".gif",".png",".js"};

        if (fileList.Contains(fileExtention)) return;

        currentUrl = DoAnyTranformation(currentUrl);
        Redirect(currentUrl);

    }

    private void Redirect(string virtualPath)
    {
        if (string.IsNullOrEmpty(virtualPath)) return;
        _context.Context.Response.Status = "301 Moved Permanently";
        _context.Context.Response.StatusCode = 301;
        _context.Context.Response.AppendHeader("Location", virtualPath);
        _context.Context.Response.End();
    }

    public void Dispose()
    {

    }

}

I think that doing a redirection could be a cleaner solution if you need to change the parameter list in your new application also dealing with a lot of routes can get ugly pretty quickly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.