ASP.NET 的友好 URL
Python 框架总是提供处理 URL 的方法,以优雅的方式传达请求的数据,例如 http ://somewhere.overtherainbow.com/userid/123424/
我希望您注意结束路径/userid/123424/
您如何在 ASP.NET 中执行此操作?
Python frameworks always provide ways to handle URLs that convey the data of the request in an elegant way, like for example http://somewhere.overtherainbow.com/userid/123424/
I want you to notice the ending path /userid/123424/
How do you do this in ASP.NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我一直在使用 Intelligencia 的 URL 重写器:
http://urlrewriter.net/
配置起来非常简单- 可能需要一个小时才能全部启动并运行。 它几乎没有什么问题......
我会推荐它,但我应该提到我没有尝试过任何其他的。
祝你好运!
I've been using a URL rewriter by Intelligencia:
http://urlrewriter.net/
It was so easy to configure - maybe an hour to get it all up and running. Very few problems with it...
I'd recommend it, but I should mentioned I've not tried any other ones.
Good luck!
另外,请查看 ASP.NET MVC,或者如果您使用 Webforms,请查看 ASP.NET 3.5 SP1 中的新 System.Web.Routing 命名空间
Also, check out ASP.NET MVC or if you're set on webforms, the new System.Web.Routing namespace in ASP.NET 3.5 SP1
我针对这个问题开发了一个开源 NuGet 库,它隐式地将 EveryMvc/Url 转换为 every-mvc/url。
虚线网址对 SEO 更友好且更易于阅读。 小写 URL 往往会产生较少的问题。 (更多内容请参阅我的博客文章)
NuGet 包:https://www.nuget.org/packages/LowercaseDashedRoute/
要安装它,只需在 Visual Studio 中打开 NuGet 窗口即可右键单击该项目并选择 NuGet Package Manager,然后在“Online”选项卡上键入“Lowercase Dashed Route”,它应该会弹出。
或者,您可以在包管理器控制台中运行此代码:
Install-Package LowercaseDashedRoute
之后,您应该打开 App_Start/RouteConfig.cs 并注释掉现有的route.MapRoute(。 ..) 调用并添加以下内容:
就是这样。 所有 url 均为小写、虚线,并且隐式转换,无需您执行任何其他操作。
开源项目网址:https://github.com/AtaS/lowercase-dashed-route
I've developed an open source NuGet library for this problem which implicitly converts EveryMvc/Url to every-mvc/url.
Dashed urls are much more SEO friendly and easier to read. Lowercase URLs tend to create less problems. (More on my blog post)
NuGet Package: https://www.nuget.org/packages/LowercaseDashedRoute/
To install it, simply open the NuGet window in the Visual Studio by right clicking the Project and selecting NuGet Package Manager, and on the "Online" tab type "Lowercase Dashed Route", and it should pop up.
Alternatively, you can run this code in the Package Manager Console:
Install-Package LowercaseDashedRoute
After that you should open App_Start/RouteConfig.cs and comment out existing route.MapRoute(...) call and add this instead:
That's it. All the urls are lowercase, dashed, and converted implicitly without you doing anything more.
Open Source Project Url: https://github.com/AtaS/lowercase-dashed-route
这是使用 ASP.NET MVC 执行此操作的另一种方法
首先,这是具有两个操作的控制器代码。 Index 从模型中获取用户列表,userid 获取单个用户:
这是 Index.asp 视图,它使用 ActionLink 以正确的格式创建链接:
这是显示个人详细信息的 userid.aspx 视图:
最后为了完整起见,这里是模型代码:
Here's another way of doing it using ASP.NET MVC
First off, here's the controller code with two actions. Index gets a list of users from the model, userid gets an individual user:
Here's the Index.asp view, it uses an ActionLink to create links in the correct format:
And here's the userid.aspx view which displays an individual's details:
And finally for completeness, here's the model code:
这是另一个示例,也使用 ASP.NET 路由来实现友好的 URL。
应用程序处理的映射示例如下:
http://samplesite/userid/1234 - http://samplesite/users.aspx?userid=1234
http://samplesite/userid/1235 - http://samplesite/users.aspx?userid=1235
此示例不使用查询字符串,但需要额外的代码 在 aspx 页面上。
步骤 1 - 将必要的条目添加到 web.config
步骤 2 - 在 global.asax 中添加路由表
定义从友好 URL 到 aspx 页面的映射,保存请求的用户 ID 供以后使用。
步骤 3 - 实现路由处理程序
将包含参数的路由上下文传递到页面。 (注意 IRoutablePage 的定义)
步骤 4 - 检索目标页面上的参数
注意 IRoutablePage 的实现。
This is an alternative example that also uses ASP.NET Routing to implement friendly URLs.
Examples of the mappings that the application handles are:
http://samplesite/userid/1234 - http://samplesite/users.aspx?userid=1234
http://samplesite/userid/1235 - http://samplesite/users.aspx?userid=1235
This example does not use querystrings but requires additional code on the aspx page.
Step 1 - add the necessary entries to web.config
Step 2 - add a routing table in global.asax
Define the mapping from the friendly URL to the aspx page, saving the requested userid for later use.
Step 3 - implement the route handler
Pass the routing context, containing the parameter, to the page. (Note the definition of IRoutablePage)
Step 4 - Retrieve the parameter on the target page
Note the implemetation of IRoutablePage.
此示例使用 ASP.NET 路由来实现友好的 URL。
应用程序处理的映射示例如下:
http://samplesite/userid/1234 - http://samplesite/users.aspx?userid=1234
http://samplesite/userid/1235 - http://samplesite/users.aspx?userid=1235
此示例使用查询字符串,避免了修改 aspx 页面上的代码的任何要求。
步骤 1 - 将必要的条目添加到 web.config
步骤 2 - 在 global.asax 中添加路由表
定义从友好 URL 到 aspx 页面的映射,保存请求的用户 ID 供以后使用。
步骤 3 - 实现路由处理程序
在路由发生之前将查询字符串添加到当前上下文。
代码来自users.aspx
aspx页面上的代码,供参考。
This example uses ASP.NET Routing to implement friendly URLs.
Examples of the mappings that the application handles are:
http://samplesite/userid/1234 - http://samplesite/users.aspx?userid=1234
http://samplesite/userid/1235 - http://samplesite/users.aspx?userid=1235
This example uses querystrings and avoids any requirement to modify the code on the aspx page.
Step 1 - add the necessary entries to web.config
Step 2 - add a routing table in global.asax
Define the mapping from the friendly URL to the aspx page, saving the requested userid for later use.
Step 3 - implement the route handler
Add the querystring to the current context before the routing takes place.
Code from users.aspx
The code on the aspx page for reference.