MVC 在什么时候会使用“漂亮的 url”?结束和“丑陋的网址”开始?
我正在使用 ASP.NET MVC,但这确实适用于任何 MVC 框架。我知道 MVC 框架的好处之一是构建“漂亮”的 url。到目前为止,我的小型应用程序一直保持简单,大多数路由使用以下常规约定:controller/action/id。但是,现在我创建了一个简单的页面,它将根据给定的查询字符串参数重定向到一个操作:
mysite.com/visitors/lookup?signAction=signin
或
mysite.com/visitors/lookup?signAction=signout
来自 Web 表单,这种构造很自然且非常常见,但恐怕我没有意识到这里漂亮网址的好处。
对于我的示例,最好创建自定义路线还是保持原样?自定义路由结束和查询字符串参数开始时的一般经验法则是什么?
I'm working with ASP.NET MVC but this really applies to any MVC framework. I understand that one of the benefits of an MVC framework is the construction of "pretty" urls. I have kept my small application simple thus far and most routes use the normal convention of: controller/action/id. However, now I've created a simple page that will redirect to an action based on the query string parameter given:
mysite.com/visitors/lookup?signAction=signin
or
mysite.com/visitors/lookup?signAction=signout
Coming from Web Forms, this construction is natural and very common, but I'm afraid I'm not realizing the benefits of pretty urls here.
For my example, would it be best to create a custom route or keep it as-is? What is the general rule of thumb for when custom routes end and query string parameters begin?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先我不会说这与 MVC 有任何关系。漂亮的网址几乎可以/应该在任何地方使用。
其次,这实际上取决于您如何使用网址。但最重要的想法是“漂亮的网址”不能替代查询参数。
例如,你
可以/应该吗?很容易转化为
基本上漂亮的网址背后的想法有两部分。
他们的主要目的是让他们中的许多人可以被记住。漂亮网址的想法来自于亚马逊 2000 多个字符怪物。
他们的次要目的是搜索引擎优化。然而,最近许多开发人员开始忘记这一点,并将各种垃圾放回到他们的网址中。
但我想说的是,这
本质上并不比
Firstly I wouldn't say this has got anything to do with MVC. Pretty urls can/should be used pretty much anywhere.
Secondly, it really depends on how you're using your urls. But the big idea is that 'pretty urls' are NOT a replacement for query parameters.
Like for example, your
Could/should? easily translate into
Basically the idea behind pretty urls two fold.
Their primary purpose is that many of them should be MEMORIZABLE. The pretty urls idea came out of things like amazon's 2000+ character monstrosities.
Their secondary purpose is SEO. However as of late many developers have started forgetting this and are putting all sorts of junk back into their urls.
But what I'm trying to say is that
is essentially no prettier than
如果你正确地设置了你的路由,它可以是:
或者只是
你必须决定要走多远,但我认为你可以比你的例子做得更好,而不用过多地调整路由结构。
If you set up your routes properly, it can be:
or simply
You'll have to decide how far to go with it, but I think you can do better than your examples without monkeying much with the routing structure.
老实说,我想说这取决于你的喜好,这就是它的伟大之处。制作任何东西都没有经验法则。好吧,也许涉及到性能,但在这种情况下,无需担心性能变化。就我个人而言,我会说创建一条新路线。这并不太困难,而且就像你说的,它的 URL 很漂亮。另外,另一个需要考虑的事情可能是隐藏值。我不完全确定您的代码布局,但如果您有隐藏的signaction输入并且值发生变化,那么您只需执行Request.Form[“signaction”]即可获取值。希望这有帮助。
Honestly, I want to say it depends on your preference, which is the great thing about it. There is no rule of thumb when it comes to making anything. Well, maybe when it comes to performance, but in this case, there is no performance change to worry about. Personally, I'd say create a new route. It's not too difficult and like you said, it's pretty URLs. Also, another thing to consider would maybe be a hidden value. I'm not fully sure of your code layout, but if you had a hidden input of signaction and the value changes, then you could just do a Request.Form["signaction"] to get the value. Hope this helps.
我们将使用
/session/new
=>登录表单/session/create
POST/session/destroy
DELETE 注销We're going with
/session/new
=> login form/session/create
POST/session/destroy
DELETE logoutURL 路由到“漂亮 URL”的主要优点是搜索引擎优化和永久链接内容。对于注销操作,这些都不是真正适用的。在这种情况下,这只是一个审美决定。
The main advantage to URL routing to "pretty URLs" is search engine optimization and permalinking content. For a logout action, neither of these are really applicable. It's merely an aesthetic decision in this case.