使用现有 Webforms 站点运行 MVC

发布于 2024-12-11 23:35:46 字数 1215 浏览 0 评论 0原文

我读过大多数(如果不是全部)有关将 MVC 3 添加到现有 Web 表单站点的各种文章。我对 webforms 站点的全局 asax 进行了更改以支持 MVC,并对 web.config 进行了更改。但仍然存在一些问题。

  1. MVC 3 是贪婪的 - 默认情况下它会接管网站的根目录,以便导航到根目录我会进入 mvc 的入口页面,而不是我想要的 Webforms 页面。为了清楚地说明导航到根目录时,我希望它转到 mysite.com/default.aspx。要修复站点根目录,我添加

    routes.MapPageRoute("SiteRoot", "", "~/Default.aspx") 然而,这会破坏 MVC url,这样我最终会得到混乱的 url,例如

    http://localhost:86/default?action=Index&controller=Blog< /a> 我该如何修复它,以便网站根目录转到 webforms default.aspx 并且我的 MVC url 仍然正确?

  2. MVC 3 破坏了 Web 表单使用的一对 HttpHandler。具体来说,我使用一个处理程序将远期过期标头添加到图像中,并使用一个资源处理程序来组合样式表和 JavaScript 文件。 RegisterRoutes 中任何路由的纯粹存在都会破坏这些处理程序,以便样式表不会通过资源处理程序加载,并且我失去了遥远的未来到期,并且有一个 httpcompression 模块也停止工作。那么,如何维护 Web 表单的 httphandlers 和模块,同时仍然保留网站的 MVC 3 部分的路由?

我在有关混合 MVC3 和 Webform 的各种博客文章中没有看到任何人解决这两个问题。

哦,我在 global.asax 中有 routes.IgnoreRoute("{resource}.aspx/{*pathInfo}") 。 资源处理程序使用 aspx 文件扩展名,所以我想在全局中排除此内容处理程序可以工作,但我想不行。

我知道的另一个选择是在 wbeforms 站点内设置一个文件夹并将其作为自己的应用程序起点运行 - 但是我最终得到一个我并不真正想要的网址,例如 site.com/blog/blog/index 其中第一个博客是作为其自己的应用程序点运行的文件夹,第二个博客是控制器。

I read have most if not all of the various articles on adding MVC 3 to existing webforms sites. I have made the changes to the webforms site's global asax to support MVC as well changes to web.config. However there are still some issues.

  1. MVC 3 is greedy - By default it takes over the root of the site so that navigate to the root i go to the entry page for mvc and not the webforms page which is what i want. To be clear when navigating to the root, i want it to go to mysite.com/default.aspx. To fix site root i add

    routes.MapPageRoute("SiteRoot", "", "~/Default.aspx")
    However this then breaks the MVC urls such that i end up with messed up urls like

    http://localhost:86/default?action=Index&controller=Blog
    How can i fix it so that the site root goes to webforms default.aspx and still have my MVC urls be correct?

  2. MVC 3 breaks a ccouple of HttpHandlers that the webform uses. Specifically, I use a handler to add far future expires headers to images and a resource handler to combine stylesheets and javascript files. The sheer presence of any route in RegisterRoutes breaks these handlers so that stylesheets dont load via the resource handler, and i lose the far future expires, and there is a httpcompression module that stops working too. So how do I maintain my httphandlers and modules for the webforms while still retaining the routes for MVC 3 portion of the site?

These 2 issues i have not seen anyone address in the various blog posts regarding mixing MVC3 and webforms.

Oh and i have routes.IgnoreRoute("{resource}.aspx/{*pathInfo}") in global.asax. The resource handler uses an aspx file extension so I would think then withthis exclusion in global that the hanlder would work but i guess not.

Another option i know is to set up a folder inside the wbeforms site and run it as its own appllication starting point - however i end up with a url i dont really want such as site.com/blog/blog/index where the 1st blog is the folder running as its own application point and the second blog is the controller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疾风者 2024-12-18 23:35:46

我能找到的最佳解决方案是将 MVC 3 与现有的 Webforms 站点混合。相反,将 MVC 目录设置为其自己的应用程序起点,然后从该文件夹运行 MVC,以便它和现有的 Webforms 应用程序完全不了解彼此。通过这种方式,资源处理程序和 httpmodules 以及其他处理程序仍然适用于 Webforms 站点。这样,默认情况下,域根目录将转到 webforms default.aspx 而不是 MVC。另外,我不必将该条目放入 global.asax 中,这反过来又搞砸了 mvc 3 url。下一个问题是处理 url,这样我就没有像 blog/blog/post/1 这样的 url。为了解决这个问题,我将 html 操作链接更改为

@Html.ActionLink(item.PostTitle, "Post", New With {.id = item.PostId}, Nothing)

然后更改 global.asax - 将默认路由更改为(只需删除 {controller}:

routes.MapRoute( _
        "Default", _
        "{action}/{id}", _
        New With {.controller = "Blog", .action = "Index", .id = UrlParameter.Optional}, New String() {"RiderDesignMvcBlog.Core.Controllers"})

所以现在而不是 blog/blog/index 或 blog/blog/post/1 i获得一个正确的干净网址,网址中没有重复的博客 - blog/post/1

The best solution that I can find is to not mix MVC 3 with the existing webforms site. Instead set the MVC directory as its own app starting point and then run the MVC from that folder so that it and the existing webforms app are completely ignorant of each other. In this fashion, the resource handler and httpmodules and other handlers all still work for the webforms site. And this way the domain root goes to webforms default.aspx and not MVC by default. Also i dont have to put that entry into global.asax which in turn screwed up the mvc 3 urls. The next issue was to deal with the urls so that i didnt have a url lke blog/blog/post/1. To fix this i changed the html action link to

@Html.ActionLink(item.PostTitle, "Post", New With {.id = item.PostId}, Nothing)

And then make a change to global.asax - change the default route to (simply delete {controller}:

routes.MapRoute( _
        "Default", _
        "{action}/{id}", _
        New With {.controller = "Blog", .action = "Index", .id = UrlParameter.Optional}, New String() {"RiderDesignMvcBlog.Core.Controllers"})

So now instead of blog/blog/index or blog/blog/post/1 i get a proper clean url without the duplicate blog in the url - blog/post/1

挽梦忆笙歌 2024-12-18 23:35:46

我建议您将处理程序的扩展名更改为真正的处理程序 .ASHX,并忽略它们。

I suggest you change the extension of your handler as a true handler, .ASHX, and ignore them instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文