在 ASP.NET MVC 应用程序中通过 URL 传递参数

发布于 2024-10-15 18:31:14 字数 779 浏览 3 评论 0原文

我需要访问一个没有名称的参数。 例如:我有一个控制器测试,我想在 mysite.com/test/data 中获取“数据”。无需调用动作数据。 我知道如何通过索引操作来传递它。

public ActionResult Index(string id)

这样我只需要输入 mysite.com/test/Index/data 即可获取“数据”,但我不想输入 Index。

有谁知道该怎么做?

编辑:

非常感谢@andyc!

AndyC 我使用了你所说的并创建了一个测试。它有效 =D

现在我可以输入 mysite.com/something,如果某些内容不是控制器,它会重定向到我的个人资料页面。

这对我有用

routes.MapRoute(
      "Profile",
      "{id}",
      new { Controller = "Profile", action = "Index", id = UrlParameter.Optional }
);

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

I need to access a parameter without name.
Ex: I have a controller test, I want to get "data" in mysite.com/test/data. Without calling the action data.
I know how to do that passing it though Index action.

public ActionResult Index(string id)

That way I'd only need to type mysite.com/test/Index/data to get "data", but I don't want to have to type Index.

Does anyone know how to do it?

EDIT:

Thanks a lot to @andyc!

AndyC I used what you said and created a test. It worked =D

Now I can type mysite.com/something and if something is not an controller it redirects to my profile page.

This is what is working for me

routes.MapRoute(
      "Profile",
      "{id}",
      new { Controller = "Profile", action = "Index", id = UrlParameter.Optional }
);

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

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

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

发布评论

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

评论(1

无人问我粥可暖 2024-10-22 18:31:14

设置自定义路由

在您的 global.asax 文件中,输入(在 RegisterRoutes 方法内):

routes.MapRoute(
    "MyShortRoute",
    "view/{id}",
    new { Controller = "test", action = "Index" }
);

第一个参数是名称,第二个参数是 URL 格式,最后一个参数是默认值(在这种情况下,如果将 id 留空,则默认为 id 0。

请注意,我不使用 test/{id},因为这也会匹配 test/Edit,其中 edit 是您不希望作为参数传递的操作(我想不出另一个避免这种情况的方法,特别是当您使用字符串而不是整数作为参数时)

请记住在 global.asax 文件中将更具体的路线放在之前不太具体路线当系统搜索要采取的路线时,它不会尝试查找最具体的匹配,而是从顶部开始,并使用找到的第一个匹配。

因此,这很糟糕:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "test", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
    "Specific",
    "test/{id}",
    new { controller = "test", action = "Index", id = 0 } 
);

在这个示例中,如果您浏览到 test/somevalue ,它将匹配第一个条目,这不是您想要的,为您提供 testcontroller 和 somevalue 操作。

(当您添加更具体的路线时,您将希望它靠近顶部,并且绝对在默认路线之前)。

Setup a custom route

In your global.asax file, put (Inside the RegisterRoutes method):

routes.MapRoute(
    "MyShortRoute",
    "view/{id}",
    new { Controller = "test", action = "Index" }
);

The first parameter is a name, the second is the URL format and the last parameter is the default values (in this case if you leave id empty it'll default to id 0.

Notice that I don't use test/{id} as this would also match test/Edit, where edit is an action that you do not want passed as a parameter (I can't think of another way to avoid this, especially if you're using strings instead of ints for your parameters).

Remember to order your routes appropriately in your global.asax file. Put more specific routes before less specific routes. When the system is searching for a route to take, it does not attempt to find the most specific match but instead it starts from the top, and uses the first match it finds.

Therefore, this is bad:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "test", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
    "Specific",
    "test/{id}",
    new { controller = "test", action = "Index", id = 0 } 
);

In this example, if you browse to test/somevalue it will match the FIRST entry, which is not what you want, giving you the testcontroller and the somevalue action.

(As your adding a more specific route, you will want it near the top, and definitely before your default).

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