更新时间:2023-02-23 10:27:59
要使用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
这是pretty多少呢。为了摆脱一个页面的URL的值,你会通过URL段需要循环(不要忘了进口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
将循环三次,并给您 122
,比尔博
和巴金斯
。
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
被击中,并具有:
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
.