asp.net mvc url 路由

发布于 2024-08-02 10:25:55 字数 129 浏览 5 评论 0原文

如何映射诸如domain.com/用户名之类的内容?问题是我认为 MVC 路由会寻找控制器来确定它应该如何处理映射请求。

我对 ASP.NET MVC 还很陌生。

然而,根据迄今为止的教程,路由机制似乎相当僵化。

How do I map something like domain.com/username? The problem is I think that the MVC routing looks for the controller to determine how it should handle the mapping request.

I am pretty new to ASP.NET MVC.

However, based on the tutorials so far, the routing mechanism seems rather rigid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

喜你已久 2024-08-09 10:25:55

它实际上非常灵活,我想如果您有更多的使用经验,您会发现它非常强大。您可以按照以下方式执行您想要的操作:

routes.MapRoute("MyRoute", 
                 "{username}", 
                 new { controller = "Home", action = "Index", username = "" });

这将选择一个默认控制器(“Home”)和一个默认操作方法(“Index”),并向其传递一个用户名参数(默认情况下设置为“”)。

但要小心这条路由,因为它几乎会匹配您能想象到的任何 URL。它应该是您添加到映射中的最后一个路由,因此您的其他路由首先有机会访问该 URL。

It's actually quite flexible, I think you'll find it's quite powerful with some more experience with it. Here's how you can do what you want:

routes.MapRoute("MyRoute", 
                 "{username}", 
                 new { controller = "Home", action = "Index", username = "" });

This chooses a default controller ("Home") and a default action method ("Index"), and passes it a username parameter (which is set to "" by default).

Careful with this route though, because it will match virtually any URL you can imagine. It should be the last route that you add to your mappings, so your other routes have a chance at the URL first.

金橙橙 2024-08-09 10:25:55

要添加到 womp - 我会走这条路线(双关语):

routes.MapRoute("MyRoute", 
                 "user/{username}", 
                 new { controller = "User", action = "Index", username = "" });

这会将 url 映射到:domain.com/user/{username}

并且您可以随时将控制器和操作更改为您想要的任何内容。

To add to womp - I would go this route (pun intended):

routes.MapRoute("MyRoute", 
                 "user/{username}", 
                 new { controller = "User", action = "Index", username = "" });

This will map urls to: domain.com/user/{username}

And you can always change the controller and action to whatever you want.

可遇━不可求 2024-08-09 10:25:55
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("MyRoute",
              "{id}",
              new { controller = "Home", action = "User", id = "" });


            routes.MapRoute(
              "Default",                                              // Route name
              "{controller}/{action}/{id}",                           // URL with parameters
              new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
          );

如果我有类似的内容,那么我可以指向 mydomain.com/userid 以及 mydomain.com/home/index/1 之类的内容。但是,如果我只是访问 mydomain.com,它会转到用于 userid 的页面,这是有道理的,因为它将它与第一个路由规则匹配并认为 id 是“”,但我该如何阻止这种行为呢?

  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("MyRoute",
              "{id}",
              new { controller = "Home", action = "User", id = "" });


            routes.MapRoute(
              "Default",                                              // Route name
              "{controller}/{action}/{id}",                           // URL with parameters
              new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
          );

If I have something like that, then I am able to point to something like mydomain.com/userid as well as mydomain.com/home/index/1. However, if I just go to mydomain.com, it is going to the page used for userid, which makes sense, because it matches it with the first route rule and thinks the id is "", but how do i stop that behavior?

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