更新时间:2023-02-23 10:09:58
要使用 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
.