简体   繁体   中英

How do I redirect WebForms URLs to their MVC equivalents in an ASP.NET MVC 4 solution?

I've found many different variations of this question, but nothing that seems to be exactly what I'm trying to attempt, so please excuse me if it has already been answered.

I have an old WebForms solution that I have completed converted to MVC 4 (C#). I have both projects in separate solutions. I want to completely remove the old WebForms project, solution, and deployed files and redeploy the new MVC 4 site in it's place. That being the case, I don't want to kill all the old URLs. For example, in the WebForms site you could go to:

http://mysite.com/Customers.aspx

in MVC 4, that URL is now:

http://mysite.com/Customers

I would like to setup a Route or a Redirect rule that handles scenarios like that. I'm even fine adding many rules manually as the site really isn't that big. I feel like this should be pretty straightforward, but I'm really new to this space and just can't quite seem to figure out where or what I should be adding.

Try using custom filters like such: (this code hasn't been tested with your scenario, but I've used my base Redirect to SSL which has been tested)...

using System.Web.Mvc;   
namespace Libraries.Web.Attributes
{
    public class RedirectASPXAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var request = filterContext.HttpContext.Request;
            if (request.Url != null && request.Contains(".aspx"))
            {
                var manipulatedRawUrl = request.RawUrl.Remove(request.RawUrl.LastIndexOf(".aspx"), 5);
                filterContext.Result = new RedirectResult("http://" + request.Url.Host + manipulatedRawUrl);
            }
        }
    }
}

Then, you simply decorate the controllers with the attributes:

[RedirectASPX]
public class HomeController : Controller
{

}

Hopefully this will at least point you in the right direction.

I finally figured this out thanks to @brenton pointing me in the right direction. Full set of steps for anybody who does this after me.

Install the URL Rewrite module in your IIS instance(s) found here:

http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module

Visual Studio's Intellisense won't know about the rewrite module so follow the instructions here to add it (not required):

https://stackoverflow.com/a/8624558/45077

After that, add the following block into the <system.WebServer> section of your Web.config file:

<rewrite>
<rules>
    <rule name="Redirect ASPX File to MVC" stopProcessing="true">
        <match url="(.*)\.aspx" />
        <action type="Redirect" url="{R:1}" appendQueryString="false" />
    </rule>
</rules>
</rewrite>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM