ASP.NET - 构建您自己的路由系统 [英] ASP.NET - Building your own routing system

查看:11
本文介绍了ASP.NET - 构建您自己的路由系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的一个项目中,我用 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 Framework 来说是一个巨大的增强,很高兴看到他们重用该代码并使其在两个框架中都能正常工作.如果您想了解更多相关信息,请查看 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 管道以及如何实现 IHttpModuleIHttpHandler接口来创建您自己的 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 - 构建您自己的路由系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆