友好的 URL 和查询字符串 [英] Friendly URLs and Query Strings
问题描述
在我的项目(ASP.NET Web 窗体)中,我想使用 友好 URL,从 NuGet 安装.
In my project (ASP.NET Web Forms) I want to use Friendly URLs, installed from NuGet.
我在 global.asax 文件中注册了 route:
I registered route in global.asax file:
Public Shared Sub RegisterRoutes(routes As RouteCollection)
routes.MapPageRoute("Route", "default/{id}", "~/default.aspx?id={id}")
End Sub
使用此代码,我可以使用 default/123 而不是 default?id=123.我想在 url 中添加分配给 id 的名称.所以我可以有这样的网址:default?123-Firstname-Lastnam.名称保存在数据库中,单列.如何将第二个参数(名称)添加到 url,添加符号 -
并显示它没有这样的字母:řčš
(因为应用程序是 Chech 语言.谢谢解答.
With this code, I can use default/123 instead of default?id=123. I want to add name, assigned to the id, in the url. So I can have url like this: default?123-Firstname-Lastnam. Name is saved in database, in single column. How can I add second parameter (name) to the url, add symbol -
and display it without letters like this: řčš
(because the application is in Chech language.
Thanks for answer.
推荐答案
要使用 FriendlyUrls,从 NuGet 安装后,转到您的 global.asax
并启用它:
To use FriendlyUrls, after you install it from NuGet, go to your global.asax
and enable it:
Imports Microsoft.AspNet.FriendlyUrls
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
RegisterRoutes(RouteTable.Routes)
End Sub
Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.EnableFriendlyUrls()
End Sub
'rest of global
差不多就是这样.要从页面的 URL 中获取值,您需要遍历 URL 段(不要忘记 Imports Microsoft.AspNet.FriendlyUrls
):
That is pretty much it. To get the values out of a URL for a page, you'll need to loop through the URL segments (don't forget Imports Microsoft.AspNet.FriendlyUrls
):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each segment As String In HttpRequestExtensions.GetFriendlyUrlSegments(Request)
Dim val As String = segment
Next
End Sub
因此访问 siteURL.com/default/123
将循环一次并返回 123
,而 siteURL.com/default/122/Bilbo/Baggins
将循环 3 次并给你 122
、Bilbo
和 Baggins
.
So visiting siteURL.com/default/123
will loop once and give you 123
, while siteURL.com/default/122/Bilbo/Baggins
will loop three times and give you 122
, Bilbo
, and Baggins
.
或者,如果您只想使用普通路由而不是 FriendlyUrls:
Or, if you just want to use plain routing and not FriendlyUrls:
routes.MapPageRoute("id-route", "default/{id}", "~/default.aspx")
路由的一个好处是您可以使用 URL 来传递变量数据, 不使用查询字符串.所以传递名称数据的路线可能看起来像
One good thing about routing is you can use the URL to pass variable data without using query strings. So the route to pass name data could look like
Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.MapPageRoute("name-route", "default/{id}/{firstName}/{lastName}", "~/default.aspx")
End Sub
然后 default.aspx 可以被 siteURL.com/default/123/Frodo/Baggins
命中,并且有:
And then default.aspx could be hit with siteURL.com/default/123/Frodo/Baggins
and has:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim id As Integer = 0
Int32.TryParse(Page.RouteData.Values("id"), id)
Dim firstName As String = Convert.ToString(Page.RouteData.Values("firstName"))
Dim lastName As String = Convert.ToString(Page.RouteData.Values("lastName"))
'do something if id > 0
End Sub
其他注意事项:如果您只想在单个列中包含名称,则可以组合使用 firstName
和 lastName
变量进行保存.使用 -
作为分隔符并不是一个好主意,因为人们可以有连字符的名字.将姓名保存在单列中往往会导致问题,因为这会使按名字或姓氏等排序变得更加困难.
Other Considerations: If you only want name in a single column, then you can combine the firstName
and lastName
variables for saving. Using -
as a delimeter like you show in question isn't a good idea, as people can have hyphenated names. Saving name in a single column tends to cause problems as it makes it much harder to sort by first or last name, etc.
此外,您似乎将从 GET
命令插入到您的数据库中.我认为使用 PUT
或 POST
会更清楚.
Also it appears you will be inserting into your database from a GET
command. I would think this would be much more clear to do using PUT
or POST
.
这篇关于友好的 URL 和查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!