友好的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 的文件:
Public Shared Sub RegisterRoutes(routes As RouteCollection)
routes.MapPageRoute("Route", "default/{id}", "~/default.aspx?id={id}")
End Sub
有了这个code,我可以使用默认/ 123 而不是默认?ID = 123 。我想补充的名字,分配到的ID,在URL中。所以我可以有网址是这样的:默认123-名字 - Lastnam。名称保存在数据库中,单列。我怎么能第二个参数(名称)添加到URL中添加符号 -
并显示它没有这样的信件: RCS
(因为应用程序是在再检查一下语言。
感谢您的回答。
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
这是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
.
这篇关于友好的URL和查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!