路由到一个aspx页面与WebForms和MVC项目 [英] Routing to an aspx page on a project with webforms and MVC
问题描述
我添加到MVC,我一直在我需要能够访问在两个周期的变化在工作作为我期待慢慢迁移项目,但是,现有的web表单的项目,但有它默认的aspx页面。
I've added MVC to an existing webforms project that I've been working on as I'm looking to slowly migrate the project, however, during the change over period I need to be able to access both, but have it default to the aspx pages.
当涉及到注册路由,我现在有这样的地方:
When it comes to registering the routing, I currently have this in place:
private void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Backup", "{*anything}", "~/Default.aspx");
routes.MapRoute(
"MVC",
"{controller}/{action}/{id}",
new { controller = "Test", action = "Index", id = 1 }
);
}
然而,当我把它设置这样,即使我把我知道指控制器/动作组合的URL(如测试和索引从这个最后一行),它仍然将用户重定向到Default.aspx页面。
However, when I have it setup like this, even if I put in a URL that I know refers to a controller/action combination (e.g. Test and Index from the last line of this), it still redirects the user to the Default.aspx page.
我试着在改变这个为以下内容:
I've tried changing this over to the following:
private void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MVC",
"{controller}/{action}/{id}",
new { controller = "Test", action = "Index", id = 1 }
);
routes.MapPageRoute("Backup", "{*anything}", "~/Default.aspx");
}
在这里,用户可以指定页面或指定一个控制器/动作,但如果他们不包括任何控制器/ aspx页面,则默认为缺省路由的MVC,而我需要它默认为网页表单的Default.aspx
Here, the user can specify a page or specify a controller/action, but if they do not include any controller/aspx page, it defaults to the default MVC route, whereas I need it to default to the webform Default.aspx.
如何将各地的情况变得这么说,如果没有指定页/控制器,它引导到〜/ Default.aspx的,如果指定了一个控制器,它引导到控制器?
How would I get around the situation so that if no page/controller is specified, it directs to ~/Default.aspx, and if a controller is specified, it directs to that controller?
推荐答案
我试着与你的场景,在我有一个现有的Web窗体应用程序,并增加了MVC它试验。基本上我所遇到的两个解决方案。
I tried experimenting with your scenario where in i have an existing web forms application and have added mvc to it. Basically i have come across two solutions.
第一::您可以忽略加入 routes.MapPageRoute
路由配置;这就是我所做的。
First: you can ignore adding routes.MapPageRoute
to route config; this is what i have done
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.EnableFriendlyUrls();
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
}
}
请注意的,我已经在默认控制器排除值。现在,即使是有火柴,如果没有控制器present该名称则请求将回落到常规ASP.Net管道寻找一个aspx页面。通过这种方法,你可以要求如的HTTP URL://本地主机:62871 /
或的http://本地主机:62871 /首页/指数
或的http://本地主机:62871 /主页/
或的http://本地主机:62871 / About.aspx
二::在这种方法中,你可以创建一个的面积在的领域的文件夹,为您新的基于MVC的工作和离开的 RouteConfig 的在 App_Start
为默认,这样它看起来像下面。
Second:: In this approach you can create an area under Areas folder for your new MVC based work and leave the RouteConfig in App_Start
to its default so that it looks like following.
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.EnableFriendlyUrls();
}
}
然后在您的区域注册类中定义的路线在下面的例子。
And then in your Area registration class define route as in following example .
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
希望有所帮助。谢谢你。
Hope that helps. Thanks.
这篇关于路由到一个aspx页面与WebForms和MVC项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!