url 中没有控制器和操作的路由

发布于 2024-10-21 08:59:13 字数 685 浏览 5 评论 0原文

如何配置 ASP.NET MVC 3 路由,使其不需要 url 中的控制器和操作?

我用它来显示项目“abc123”的详细信息,并使用默认路由设置进行配置:

mysite.com/Home/Details/abc123

值“abc123”是一个唯一值,家庭控制器使用它来在数据库中查找正确的项目。

但我更喜欢使用这些超短网址:

mysite.com/abc123

该网站还将有很少的附加控制器,例如“关于”和“联系方式”。我假设我还必须专门配置这些,因此默认控制器不会开始查找 ID 为“关于”或“联系人”的项目的详细信息。我该怎么做?

更新:

这就是我的路线最终的样子:

routes.MapRoute("About", "About", New With {.controller = "About", .action = "Index"})
routes.MapRoute("ID", "{id}", New With {.controller = "Home", .action = "Details"})
routes.MapRoute("Default", "", New With {.controller = "Home", .action = "Index"})

:)

How do I configure ASP.NET MVC 3 routing so it doesn’t require the controller and action in the url?

I have this to display the details for item “abc123”, configured with the default routing-settings:

mysite.com/Home/Details/abc123

The value "abc123" is a unique value which the Home controller uses to look up the correct item in a database.

But I would prefer to have these ultra-short urls instead:

mysite.com/abc123

The site will also have very few additional controllers, such as “About” and “Contact”. I assume I would have to configure those specifically as well, so the default controller doesn’t start looking for details for an item with ID “About” or “Contact”. How do I do that?

UPDATE:

Here's what my routes ended up looking like:

routes.MapRoute("About", "About", New With {.controller = "About", .action = "Index"})
routes.MapRoute("ID", "{id}", New With {.controller = "Home", .action = "Details"})
routes.MapRoute("Default", "", New With {.controller = "Home", .action = "Index"})

:)

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

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

发布评论

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

评论(3

心碎的声音 2024-10-28 08:59:13

尝试这样的操作:

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

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

第一个路由应确保您可以将 /Site/About 和 /Site/Contact 分别映射到 AboutController 和 ContactController。重要的是首先要对此进行映射。

第二条路线将确保 /abc123 映射到 ID 为“abc123”的详细信息操作

Try something like this:

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

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

The first route should ensure that you can have /Site/About and /Site/Contact mapped to AboutController and ContactController respectively. It is important that this gets mapped first.

The second route will make sure that /abc123 gets mapped to the details action with id "abc123"

亚希 2024-10-28 08:59:13

你可以用这个。

routes.MapRoute("abc123"
              , "Home/Details/{name}"
                , new { controller = "Home", action = "Detail", name = ""});

你的行动看起来像:

public ActionResult Detail(string name)
{
  //...
}

you can use this.

routes.MapRoute("abc123"
              , "Home/Details/{name}"
                , new { controller = "Home", action = "Detail", name = ""});

and your action looks like:

public ActionResult Detail(string name)
{
  //...
}
2024-10-28 08:59:13

您还可以执行以下操作:

routes.MapRoute("abc123", "abc123", new { controller = "Home", action = "Details"});

我正在使用 MVC3,它对我有用。

You can also do something like this:

routes.MapRoute("abc123", "abc123", new { controller = "Home", action = "Details"});

I'm using MVC3 and it works for me.

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