Reputation: 4027
I am converting an asp.net webforms website type project to an asp.net mvc web application. I want to keep all the changes to a minimum.
The pages and its subfolders are right at the top of the project folder and there are hundreds of them.
Now, in the new web application, I want to move them to a subfolder, let's call it WebForms.
Is there a way at runtime to run the pages as they ran before, i.e. at the root of the application folder?
Before I had: http://localhost:54321/Page1.aspx. Page1.aspx was stored in the website project root folder.
In the new project structure I have the disk:
<project folder>
WebForms
Page1.aspx
This works: http://localhost:54321/WebForms/Page1.aspx, but I want to somehow map it to http://localhost:54321/Page1.aspx.
Is it doable? I use IIS Express for development and IIS 7.5 for test/production deployment. I want to avoid having to change the image and other content urls - as you can imagine moving the pages to the subfolder breaks some of them.
Thanks
Upvotes: 2
Views: 681
Reputation: 3540
If you just want to map all requests to /something.aspx
so they go instead to WebForms\something.aspx
, you can probably just use the following route rule.
routes.MapPageRoute(
"Other Web Pages",
"{pagename}.aspx",
"~/WebForms/{pagename}.aspx");
Alternately, if you need more advanced scenarios, you could use a custom class that derives from RouteBase
and use a RegEx
to match for and map the route, similar to this question's answer
public class WebFormsRoute : RouteBase
{
Regex re = new Regex(@"^/(?<page>\w+)\.aspx", RegexOptions.IgnoreCase);
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var data = new RouteData();
var url = httpContext.Request.FilePath;
if (!re.IsMatch(url))
{
return null;
}
var m = re.Match(url);
data.RouteHandler = new PageRouteHandler("~/WebForms/" + m.Groups["page"].Value + ".aspx");
return data;
}
}
And then add it to your route collection in RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new WebFormsRoute());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Upvotes: 1