Nate
Nate

Reputation: 30656

Maintain URL when going from ASP.NET WebForms to ASP.NET MVC

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.

Upvotes: 1

Views: 287

Answers (2)

pedrommuller
pedrommuller

Reputation: 16066

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.

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

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,...){}
}

Upvotes: 4

Related Questions