在最近的一个项目中,我用 PHP 构建了自己的 MVC 框架.我实现的其中一件事是路由系统.我使用 Apache 的 mod_rewrite 将所有请求发送到 index.php,然后解析 URI 以提取信息并路由请求.
In a recent project, I built my own MVC framework in PHP. One of the things I implemented was a routing system. I used Apache's mod_rewrite to send all requests to index.php, and then parsed the URI to extract information and route the request.
我现在正在涉足 ASP.NET,我想知道是否/如何执行类似的操作.有没有办法将所有请求(类似于 WordPress 的方式)路由到执行中央路由处理的一个页面?我知道 ASP.NET 的 MVC 框架,但我想自己尝试一下,因为我正在修补和学习.
I'm dabbling in ASP.NET now, and I'm wondering if/how I might perform something similar. Is there a way to route all requests (similar to the way WordPress does it) to one page where central route processing is performed? I'm aware of the MVC framework for ASP.NET, but I'd like to take a stab at this myself as I'm tinkering around and learning.
顺便说一句,我的托管服务提供商运行 IIS 6
BTW, my hosting provider runs IIS 6
这将是一个很长的答案,因为我想确保您完全了解可以完成您想做的事情的所有方法.
This is going to be a long answer, because I want to make sure you are fully aware of all the ways you can accomplish what you want to do.
支持 ASP.NET MVC 框架的路由引擎将与传统的 ASP.NET 框架一起使用.您可以利用 RouteTable 并分配路由,就像在 ASP.NET MVC 应用程序中一样.您只是没有在传统的 ASP.NET 站点中获得 MVC 部分.这对 ASP.NET 框架来说是一个巨大的增强,很高兴看到他们重用该代码并使其在两个框架中都工作.如果您想了解更多信息,请查看 ScottGu 的 post 并向下滚动到 URL 路由改进.这里还有一个 参考使用 Phil Haack 在 WebForms 中的 System.Web.Routing.
The routing engine that powers the ASP.NET MVC Framework will work with the traditional ASP.NET Framework. You can take advantage of using the RouteTable and assigning routes, just like you would in an ASP.NET MVC application. You just don't get the MVC portion in traditional ASP.NET sites. That was a huge enhancement for the ASP.NET Framework and it was great to see them reuse that code and make it work in both frameworks. If you want to learn more about this, check out ScottGu's post and scroll down to URL Routing Improvements. Also here is a reference on how to use the System.Web.Routing in WebForms by Phil Haack.
现在,如果您仍然想自己编写.您将需要学习 ASP.NET HTTP 管道以及如何实现 IHttpModule 和 IHttpHandler接口来创建您自己的 HttpModule 或 HttpHandler 类来处理您的路由.这些接口是编写自己的路由引擎的关键.为了帮助将这些接口放在一个工作示例中,我不推荐此 MSDN 文章 够了.它向您展示了如何使用任一界面,并解释了创建您自己的路由/url 重写引擎时的差异.
Now, if you still want to write you own. You will need to learn the ASP.NET HTTP pipeline and how to implement the IHttpModule and the IHttpHandler interfaces to create your own HttpModule or HttpHandler class to handle your routing. These interfaces are the key in writing your own routing engine. To help put those interfaces in a working example, I couldn't recommend this MSDN article enough. It shows you how to with either interface and explains the differences when creating your own routing/url rewriting engine.
现在,如果您发现这对您来说可能太多了.您可以使用已经在 .NET 中编写路由/url 重写引擎的人使用第三方库.这是我不久前看到的一个问题,询问What Url rewriter你使用 ASP.Net 吗?"就在这里.
Now, if you find out that this might be to much for you. There are third party libraries you can use of people who already wrote a routing/url rewriting engine in .NET. Here is a question that I saw not to long ago asking "What Url rewriter do you use for ASP.Net?" right here on SO.
这篇关于ASP.NET - 构建自己的路由系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!