如何在混合MVC和Web窗体航线网页 [英] How to route web pages on a mixed MVC and Web Forms
问题描述
我创建了一个混合的MVC和Web Forms网站 - 很容易做到与后来的Visual Studio 2013工具。一切运作良好,我可以正确地定位到两个MVC和Web窗体页。
I have created a mixed MVC and Web Forms web site - very easy to do with the later Visual Studio 2013 tooling. All works well and I can navigate to both MVC and Web Forms pages correctly.
我想不过是什么做的,就是把我所有的Web窗体页面在一个特定的子目录,然后路线,他们不使用子目录。
What I would like to do however, is to put all of my Web Form pages in a specific sub-directory and then route to them without using the sub-directory.
/
+ Content
+ Controllers
+ Models
+ Scripts
+ Views
+ WebPages
+ Default.aspx
+ MyWebForm.aspx
所以我想能够访问:
So I would like to be able to access:
/WebPages/Default.aspx as /Default.aspx or even just /
/WebPages/MyWebForm.aspx as /MyWebForm.aspx
这可能吗?
任何意见将是AP preciated。
Any advice would be appreciated.
推荐答案
就为出发点,我们可以添加具体路线为App_Start / RouteConfig.cs web表单页面:
Just as a starting point, we can add specific routes for webforms pages in App_Start/RouteConfig.cs:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//specific page route
routes.MapPageRoute("forms", "forms", "~/Webforms/RootForm.aspx");
//specific pattern to a directory
routes.MapPageRoute("webformsmap", "{page}.aspx", "~/Webforms/{page}.aspx");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
修改
经过一番研究,我发现你在寻找什么。
我已经创建一个有用的自定义IRouteHandler以达到更好的funcionality。这样,您就可以在Web表单* .aspx页的整个目录映射到一个单一的路线。检查出来:
After some research, I've found exactly what you're looking for. I've create a useful custom IRouteHandler to achieve a better funcionality. This way you can map an entire directory of Webforms *.aspx pages to a single route. Check it out:
public class DirectoryRouteHandler : IRouteHandler
{
private readonly string _virtualDir;
public DirectoryRouteHandler(string virtualDir)
{
_virtualDir = virtualDir;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var routeValues = requestContext.RouteData.Values;
if (!routeValues.ContainsKey("page")) return null;
var page = routeValues["page"].ToString();
if (!page.EndsWith(".aspx"))
page += ".aspx";
var pageVirtualPath = string.Format("{0}/{1}", _virtualDir, page);
return new PageRouteHandler(pageVirtualPath, true).GetHttpHandler(requestContext);
}
}
通过使用DirectoryRouteHandler,你可以pretty实现自己的目标。
By using DirectoryRouteHandler, you can pretty achieve your goal.
网址〜/ SomePage.aspx页面将被映射到〜/的WebForms / SomePage.aspx页面
url "~/somepage.aspx" will be mapped to "~/WebForms/somepage.aspx"
routes.Add("rootforms", new Route("{page}.aspx",
new DirectoryRouteHandler(virtualDir: "~/WebForms")));
网址〜/表格/ SomePage的将被映射到〜/的WebForms / SomePage.aspx页面
url "~/forms/somepage" will be mapped to "~/WebForms/somepage.aspx"
routes.Add("webforms", new Route("forms/{page}",
new DirectoryRouteHandler(virtualDir: "~/WebForms")));
这篇关于如何在混合MVC和Web窗体航线网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!