MVC 在什么时候会使用“漂亮的 url”?结束和“丑陋的网址”开始?

发布于 2024-09-14 18:00:26 字数 422 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

z祗昰~ 2024-09-21 18:00:26

首先我不会说这与 MVC 有任何关系。漂亮的网址几乎可以/应该在任何地方使用。

其次,这实际上取决于您如何使用网址。但最重要的想法是“漂亮的网址”不能替代查询参数。

例如,你

mysite.com/visitors/lookup?signAction=signin

可以/应该吗?很容易转化为

mysite.com/signin

基本上漂亮的网址背后的想法有两部分。

他们的主要目的是让他们中的许多人可以被记住。漂亮网址的想法来自于亚马逊 2000 多个字符怪物。

他们的次要目的是搜索引擎优化。然而,最近许多开发人员开始忘记这一点,并将各种垃圾放回到他们的网址中。

但我想说的是,这

mysite.com/articles/page/2

本质上并不比

mysite.com/articles?page=2

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

mysite.com/visitors/lookup?signAction=signin

Could/should? easily translate into

mysite.com/signin

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

mysite.com/articles/page/2

is essentially no prettier than

mysite.com/articles?page=2
路弥 2024-09-21 18:00:26

如果你正确地设置了你的路由,它可以是:

mysite.com/visitors/lookup/signin

或者只是

mysite.com/visitors/signin

你必须决定要走多远,但我认为你可以比你的例子做得更好,而不用过多地调整路由结构。

If you set up your routes properly, it can be:

mysite.com/visitors/lookup/signin

or simply

mysite.com/visitors/signin

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.

世态炎凉 2024-09-21 18:00:26

老实说,我想说这取决于你的喜好,这就是它的伟大之处。制作任何东西都没有经验法则。好吧,也许涉及到性能,但在这种情况下,无需担心性能变化。就我个人而言,我会说创建一条新路线。这并不太困难,而且就像你说的,它的 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.

南街九尾狐 2024-09-21 18:00:26

我们将使用 /session/new =>登录表单 /session/create POST /session/destroy DELETE 注销

We're going with /session/new => login form /session/create POST /session/destroy DELETE logout

爱你不解释 2024-09-21 18:00:26

URL 路由到“漂亮 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文